Detailed explanation of springCache annotations

1. First, springCache needs to import some dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

2. Detailed explanation of relevant attention:

2.1、@Cacheable:

2.1.1 @Cacheable attribute:

  Cacheable caches the results of the method; if you want the same data in the future, you can get it directly from the cache without calling the method.
  CacheMananger : Manage multiple cache components. The real crud operation of the cache is in the Cache component. Each cache component has its own unique name.
  Attribute description:
      cacheNames/value : Specify the name of the cache component (that is, which cache to put in In), it is an array method, you can specify multiple
     keys : the key used for the cache data, you can specify it, and the value of the method parameter is used by default (you can use the spel expression, such as #id, which is the value of the parameter id)
      keyGenerato r : The key generator, you can specify the component id of the key generator yourself
      (choose one of key and keyGenerator)
      cacheManager : specify the cache manager or the resolver cacheResolver of the specified cache;
      condition : specify the cache only when the conditions are met, Only when the specified condition is true is cached
     unless : Negative cache (equivalent to unless), but unless the specified condition is true, the return value of the method will not be cached; (you can get the result to judge when using it)
      sync : whether to use asynchronous mode.

2.1.2 @Cacheable running process :
  1. Before the method runs, first query the cache (cache component) and get it according to the name specified by cacheNames;
  (CacheManager obtains the corresponding cache first), the first time the cache is obtained, it will be created automatically if there is no cache component.
  2. Go to the cache to find the content in the cache and use a key. The default is the parameter of the method
     (key is generated according to a certain strategy, and the
     keyGenerator is used by default to generate the key) SimpleKeyGenerator generates the key default strategy:
          if there is no parameter, key = new SimpleKey()
          If there is a parameter, key = the value of the parameter
          If there are multiple parameters, key = new SimpleKey(params)
  3. Call the target method if
  it is not found. 4. Call the target method if the cache is not found. Return the result of the target method and cache.

Remarks: Before the method marked by @Cacheable is executed Will first query whether there is this data in the cache, the default is to query the cache according to the value of the parameter as the key, if not, run the method, and put the returned result into the cache

2.1.3 Core :

       1. Use cacheManage (the default is ConcurrentMapCacheManager) to get the Cache (default is ConcurrentMapCache) component by name         

       2. The key is generated using keyGenerator, the default is SimpleKeyGenerator to generate the key

       3. Get the cache, create a cache component if not

2.2 、@CachePut

@CachePut not only calls the method, but also updates the cache and updates the cache while modifying the data database

1. The setting needs to update the cache component name

2. Set the key that needs to be updated (this must be consistent with the key generation strategy used when the cache was created, before the corresponding update cache can be updated, otherwise it will be invalid), the key here can use two methods:

           Method 1: Use the parameter form key = "#department.id"

           Method 2: Use the return value result key = "#result. Parameter name"
 Its attributes are the same as
@Cacheable related attributes @Cacheable can not use #result, because this is the return result can only be used, but @Cacheable must use the key before executing the method , So no  

 

2.3 、 @ CacheEvict

@CacheEvict Clear cache

key : Specify the key value to clear the data

cacheNames : Specify the name of the cache component

allEntries = true : Clear all data under the cache component (default is false)

beforeInvocation = false : Whether to clear the cache before the method is executed (the default is to clear the cache after the method is executed (ie false), after the method is executed, the cache will be cleared after the method reports an exception, the cache will not be cleared)

beforeInvocation = true : Indicates that the cache is cleared before the method is executed, regardless of whether the method is executed successfully or not

 

2.4 、@Caching

@Caching defines complex caching rules

如:@Caching( cacheable = { @Cacheable(cacheNames = "department", key = "#id") }, put = { @CachePut(cacheNames = "department", key = "#departmentName") })

 

2.5 @CacheConfig

@CacheConfig defines the name of the cache component, key generation strategy, cache manager, and cache resolver globally

Such as global definition of cache component name: @CacheConfig(cacheNames = "department")

Guess you like

Origin blog.csdn.net/D_J1224/article/details/108482632