Spring Cache caching technology, the use of Cacheable, CachePut, CacheEvict, Caching, CacheConfig annotations

Prerequisite knowledge:

  • There are two major components in the Spring Cache cache, CacheManager and Cache. There can be multiple CacheManagers in the entire cache, and they are responsible for managing the Cache inside them. Multiple Caches can be created in one CacheManager, and each Cache is responsible for storing a type of data. For example, Salary Cache is responsible for storing Salary-related data.

step:

1. Enable annotation-based caching

Add the @EnabeleCaching annotation to the startup class or other configuration classes

@SpringBootApplication
@MapperScan("com.xj.springboot.mapper")
@EnableCaching
public class SpringbootCacheApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootCacheApplication.class, args);
    }

}

2. Mark the cache annotation on the method

1.@Cacheable

This annotation can be marked on the method. When the method marked by the @Cacheable annotation is called for the first time, the annotation will store the return value of the method as a value in the cache. If no key is specified, the default value of the key is the actual value of the method . Parameter (note that it is not a formal parameter). Then call this method again, the parameters passed in are the same as last time, the code in the method body will not be executed, but the data will be directly fetched from the cache, and then returned.

There are 9 attributes in the cacheable annotation: value, cacheNames, key, keyGenerator, cacheManager, cacheResolver, condition, unless, sync. Among them, value and cacheNames are aliases for each other, so it can be said that there are only 8 attributes.

  • value / cacheNames: used to specify the name of the Cache, this property is an array type, you can specify the names of multiple caches. This attribute must be specified, and an error will be reported if it is not set.
  • key: used to specify the key of the data stored in the cache (because the return value of the current method should be stored in the cache in the form of key-value), if not set, the default is the value of the actual parameter of the incoming method (note that it is not a form reference), if you want to set it, you usually use spEL expression (spEL can refer to the figure below ) .

    Note: #result cannot be used in the key attribute of the @Cacheable annotation to retrieve the return value after the method is executed, because the @Cacheable annotation is first searched in the cache. When the method is executed, #result cannot get the value before the method is executed. of.

                

  • keyGenerator: The generator used to specify the key. Note that only one keyGenerator and key can exist at the same time. If the key is set, the keyGenerator cannot be set again.

  •  cacheManager: Specifies which CacheManager (cache manager) to use. The automatic configuration of springboot adds a cacheManager of ConcurrentMapCacheManager type to the container. If we do not specify the cacheManager, it will use the ConcurrentMapCacheManager provided by springboot by default.
  • cacheResolver: same as cacheManager.
  • condition: used to specify an expression, when the expression is true, the return value of the method is put into the cache. Expressions are usually written like spEL expressions.
  • unless: The effect is opposite to the condition attribute. Specify an expression, when the expression is true, the return value of the method will not be put into the cache. One difference from condition is that in the expression of unless, the return value of the method can be obtained for judgment.
  • sync: Whether to use asynchronous mode. If you set this annotation to true (that is, enable asynchronous mode), the unless attribute cannot be used. This property can be enabled when multiple threads are concurrent.

Summary of execution process:

 Before calling the method, it will first take the name specified by the cacheNames attribute in the annotation to check whether there is a cache with that name in the cache. If there is, it will directly return the data whose key is the actual parameter of the method. If not, then create a cache with the name specified by the cacheNames attribute, then execute the method body, and put the return result of the method as value in the cache.

2.@CachePut

This annotation can be marked on the method. When the method marked by @Cacheput annotation is called every time, the annotation will store the return value of the method as value in the cache. It is generally marked on the method that performs the database update operation.

The difference between CachePut and Cacheable annotations: Cacheable will put the method return value in the cache only when the method is called for the first time, and the method body will not be executed if it is in the cache when it is called again. And CachePut puts the return value of the method into the cache every time it calls the method.

There are 9 attributes in the cachePut annotation, which are the same as the attributes of the Cacheable annotation.

3.CacheEvict

Evict in cacheevict means expulsion, eviction. So this annotation is used to delete the data of the specified key in the cache. This annotation can be marked on the method. When the method marked by the @CacheEvict annotation is called, the annotation will find the value corresponding to the key in the cache according to the key specified by the key attribute in the Cacheevict annotation, and then delete it.

The Cacheevict annotation has 9 attributes: value, cacheNames, key, keyGenerator, cacheManager, cacheResolver, condition, allEntries, beforeInvocation. Among them, the first 7 attributes are the same as the attributes in @Cacheable, so I won’t go into details below, only introduce allEntries and beforeInvocation

  • allEntries: When allEntries is set to true, it means that calling the method marked by the current annotation will clear all cached data in the cache specified by cacheNames . Default is false
  • beforeInvocation: When beforeInvocation is set to true, the action of clearing cached data will be executed before the method is called. The default value is false, that is, the method is called first, the content in the method is executed, and finally the action of clearing the cached data is executed. Maybe you are still wondering what is the use of beforeInvocation, and what is the difference between the action of clearing cached data before the method call and after the method call. The answer is definitely different. The difference is that if the action of clearing the cached data is executed after the method is called, the method will be executed first. If an exception occurs in the method at this time, the action of clearing the cached data will not be executed again. The action of clearing the cached data is before the method is called, so the action of clearing the cached data will be executed regardless of whether the method will be abnormal or not.

4.@Caching

The beginning of this annotation is a combination of the three annotations Cacheable, CachePut, and CacheEvict. This annotation is suitable for caching complex methods. Below is the source code of the Caching annotation.

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Caching {
    Cacheable[] cacheable() default {};

    CachePut[] put() default {};

    CacheEvict[] evict() default {};
}

5.@CacheConfig

This annotation can be marked on the class to extract the public configuration of the cache.

If the three annotations of Cacheable, CachePut, and CacheEvict on all methods in a class all have cacheNames pointing to the same or the same batch of caches, then you can use @CacheConfig to annotate these cacheNames, and then Mark this annotation on the class. Then, there is no need to specify the cacheNames attribute for the cache-related methods under this class, and the cacheNames specified on the CacheConfig will be used.

Guess you like

Origin blog.csdn.net/listeningdu/article/details/128803392