SpringBoot+redis: Detailed explanation of the use of internal cache and Cache annotation

With the accumulation of time, the number of users of the application continues to increase, and the scale of data is also getting larger and larger. Often database query operations will become a bottleneck that affects the user experience. At this time, using cache is often one of the very good means to solve this problem. . Spring 3 began to provide powerful annotation-based caching support, which can add caching functions to the original Spring applications with low intrusion through annotation configuration methods to improve data access performance.

For the cache support in Spring Boot, a series of automatic configurations are provided, so that we can use the cache very conveniently. Below we use a simple example to show how we can add caching to an existing application.

1. Create a User entity class

2. Add configuration file

3. Create redis configuration file

4. Create a business realization

 

# 

CacheConfiguration Annotation Detailed Explanation # @CacheConfig: Mainly used to configure some common cache configurations that will be used in this class. Here @CacheConfig(cacheNames = "users"): It is configured that the content returned in the data access object will be stored in a cache object named users. We can also configure the cache set directly through @Cacheable without using this annotation. Name to define. 
# @Cacheable: Configured that the return value of the findByName function will be added to the cache. At the same time, when querying, it will be fetched from the cache first, and if it does not exist, then the access to the database will be initiated. This annotation mainly has the following parameters 
    1, value, and cacheNames: two equivalent parameters (cacheNames is a new addition to Spring 4, as an alias for value), used to specify the name of the set of cache storage. Due to the new @CacheConfig in Spring 4, the value attribute that was originally required in Spring 3 has also become a non-essential item. 
    2. Key: the key value of the cache object stored in the Map collection. It is not necessary. The default is according to the function. As the key value, if you configure it yourself, you need to use the SpEL expression, such as: @Cacheable(key = "#p0"): Use the first parameter of the function as the key value of the cache. More details about the SpEL expression For the content, please refer to the official document 
    3. condition: The condition of the cached object, it is not necessary, and SpEL expression is also required. Only the content that meets the expression condition will be cached, for example: @Cacheable(key = "#p0", condition = "#p0.length() <3"), which means that it will be cached only when the length of the first parameter is less than 3. If you do this configuration, the above AAA users will not be cached, readers can experiment by themselves
    4. Unless: Another cache condition parameter, not necessary, and SpEL expression is required. It is different from the condition parameter in its judgment timing. The condition is judged after the function is called, so it can be judged by the result 
    5. keyGenerator: used to specify the key generator, not necessary. If you need to specify a custom key generator, we need to implement the org.springframework.cache.interceptor.KeyGenerator interface and use this parameter to specify it. It should be noted that: this parameter and key are mutually exclusive. 
    6. CacheManager: Used to specify which cache manager to use, it is not necessary. Only when there are more than one need to use 
    7. cacheResolver: used to specify which cache resolver to use, not necessary. You need to implement your own cache resolver through the org.springframework.cache.interceptor.CacheResolver interface, and use this parameter to specify 
# @CachePut: Configured on the function, it can be cached according to the parameter definition conditions, which is different from @Cacheable, It will really call a function every time, so it is mainly used for data addition and modification operations. Its parameters are similar to @Cacheable. For specific functions, please refer to the analysis of @Cacheable parameters above 
# @CacheEvict: Configured on functions, usually used in delete methods to remove corresponding data from the cache. In addition to the same parameters as @Cacheable, it also has the following two parameters: 
    1. allEntries: not required, the default is false. When true, all data will be removed. 
    2. BeforeInvocation: not required, the default is false, and the data will be removed after the method is called. When true, the data will be removed before calling the method

github: Project address  https://github.com/juejuedog/SpringBootDemo.git

 

## Reference 

-spring-data-redis official document: https://docs.spring.io/spring-data/redis/docs/2.0.1.RELEASE/reference/html/-redis 
document: https://redis. io/documentation 
-Redis Chinese documentation: http://www.redis.cn/commands.html

Guess you like

Origin blog.csdn.net/A___B___C/article/details/107348906