Spring Boot cache trip (a)

Use Spring Cache

1.Spring Cache Profile

Spring Cache is a new technology introduced after Spring3.1. Well it did not properly store the data cache, the core idea is this: When we call a caching method, this method will result as a parameter and return value pairs stored in the cache, wait until the next using the same It will no longer execute when the method parameter to call the method, but get the results directly from the cache, and returns, enabling caching function. Spring Spring Cache use and for the use of similar transaction management, you can use annotation-based or XML-based configuration.

2. Spring Cache annotation-based learning

In Spring provides three annotations to use Spring Cache, respectively, are described below.

① @ Cacheable comment

: Used to mark the cache, which is the position of use @Cacheable annotations cache.

Cacheable may be marked on a class or method, when the method of marking, indicates that this method supports cache: when the class marking, indicates that the current class to support caching all methods

With the support of Spring Cache environment, the method using a cacheable mark, Spring will be based on key elements of the query cache whether the same key of the current Cache, if there is, it is no longer the method is performed before each method call, but available directly from the cache the results returned, else the method and returns the result into the specified cache.

In use when @Cacheable often used with three attributes for use, were introduced as follows.

· Value: When using @ Cacheable annotation, val property is required to be specified, this attribute is used to specify the name of the Cache, which means that the current return value for the cache on which cache

Key · : and the same name, is used to specify attributes corresponding to the cache key.key not have to specify if we do not specify a key, spring will use the default strategy generated key for us, where the default policy is such a requirement: If the current caching method takes no parameters, then the current key is 0; if the current buffer method has one parameter, the parameter values in key; if the current method of a plurality of cache parameters, then the key is hashcode values for all parameters. Of course, we can also be specified by EL expressions Spring provides key current cache method. In general, we can specify the key parameters of the current method of caching, usually "# parameter name", if the parameter is an object, you can attributes of these objects designated key, for example:

@Cacheable(value = "users", key = "#user.id")
    public User findUser(User user){
        return new User();
    }

Meanwhile, Spring Framework also provides a root object to our use key attributes can be ignored when specifying key attributes #root
because Spring is the default property of the root object of the calls, which were built below the root object several properties
(1) method Name : the name of the current method, key value # rootmetho name or methodName..
(2) method: Specifies the current method, key value # root.method.name or method.name
(. 3) target: this object is invoked, key # root.target value or target
(. 4) targetClass:. the current object class is called, key value #root targetClass or targetClass
(. 5) Angs:. array parameters consisting of the current method, key value #roo args [ o] or args [0]
(. 6) caches: the cache, Key # root.caches value is called the current method used [0] .name or caches [0] .name②
· for condition Condition : mainly used to specify the currently cached trigger conditions, in many cases may not need to use all caching method for caching, so Spring cache provides this property for us to exclude certain circumstances. E.g

@Cacheable(value = "users", key = "#user.id",condition="#user.id%2==0")
    public User findUser(User user){
        return new User();
    }

② @ CachePut comment

From the point of view name, @CachePut method for simply marking the return value of the annotation in the memory cache, whether the cache contains current cache, but in the form of an execution result into the key buffer. In use, @ CachePut annotations and notes @Cacheable consistent, not specifically described here

③ @ CacheEvict comment

Spring Cache provides @CacheEvict notes to clear the cache data, and @Cacheable similar, but the current method of clearing the cache when @CacheEvict a method for clearing the cache when the current class all methods class, @ CacheEvict in addition to providing @CacheEvict outside the same three attributes, it also provides a common attribute allEntries, the default value of this property is false, if the specified attribute is true, the current value will clear all cached values.

3. formal learning

3.1 configuration dependency

3.2 @EnableCaching open the cache on startup class

Written Controller 3.3

@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

//localhost:8080/saveUser?id=1&userName=dalaoyang&userPassword=123   
    @GetMapping("/saveUser")
    @CachePut(value = "user", key = "#id")
    public User saveUser(Long id, String userName, String userPassword){
        User user = new User(id,userName, userPassword);
        userRepository.save(user);
        return user;
    }
  //localhost:8080/queryUser?id=1
    @GetMapping("/queryUser")
    @Cacheable(value = "user", key = "#id")
    public Optional<User> queryUser(Long id){
        return userRepository.findById(id);
    }
  //localhost:8080/deleteUser?id=1
    @GetMapping("/deleteUser")
    @CacheEvict(value = "user", key = "#id")
    public String deleteUser(Long id){
        userRepository.deleteById(id);
        return "success";
    }
  //localhost:8080/deleteCache
    @GetMapping("/deleteCache")
    @CacheEvict(value = "user", allEntries = true)
    public void deleteCache() {
    }
}
After testing, the results have been.

Guess you like

Origin www.cnblogs.com/xc-xinxue/p/12417918.html