当前位置:网站首页>Sqel easy to use

Sqel easy to use

2022-06-12 03:59:00 Black humor 595

sping cache

spEL(Spring Expression Language) Cache definition key And all kinds of condition .

spring Cache utilize AOP The agent ends to implement
spring Provides CacheManage , You can configure the redis cache .

annotation effect Parameter description
@Cacheable Configure the method , Cache method results value: Cache name stay spring Defined in the configuration file
key: The cache key , Can be empty or specify SpEL. By default, all parameters of the method are combined
condition: Cache conditions , Can be null .SpEL Write back true、fasle ,true Cache when
@CachePut Method configuration , Cache the results after calling the method The parameters are the same as @Cacheable equally
@CacheEvict Method configuration , Delete cache conditionally Parametric pain @CAcheable equally
allEntries: Whether to empty all cache contents , Default fasle
beforeInvocation: Whether to clean up the method before it executes , Default fasle ( Method will not be cleared if it is abnormal )

SpEL Introduce

@Cacheable(value = "users" , key = "#user.userName" , condition = "#user.age < 35")
User getUserList();
name Location describe Example
methodNameroot object Name of the currently called method #root.methodnaame
methodroot object Currently called method #root.method.name
targetroot object Currently called target object instance #root.target
targetClassroot object The class of the target object being called #root.targetClass
argsroot object The parameter list of the currently called method #root.args[0]
cachesroot object The cache list used by the current method call #root.caches[0].name
Argument Execution context The currently called method parameter ,
method( User u ) adopt #u.name obtain
#u.name
result Execution context Method returns a value after execution ( After the method is executed, it is judged to be valid )
@CacheVict (beforeInvocation = false)
#result
// Add cache on interface 
public interface CacheService {
    

    @Cacheable(value = "userList" , key = "methodName")
    List<User> list();

    @Cacheable(value = "user",key = "'user'.concat(#id.toString())")
    User findUserById(Long id);

    // Here only modify  3  Number 
    //@CachePut(value="user",key="'user'.concat(#user.name.toString())")
    @CachePut(value="user",key="'user'.concat(3)")
    User update(User user);

    @CacheEvict(value = "user", key = "'user'.concat(#id.toString())")
    void remove(Long id);
}

Bottom

原网站

版权声明
本文为[Black humor 595]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120356068537.html