Introduction to Spring Boot caching annotations


This article introduces some annotations and corresponding attributes that are applied when using Spring cache, and introduces its related functions in more detail.

1. @EnableCaching annotation

@EnableCaching is provided by the spring framework, and SpringBoot mining construction inherits this annotation. This annotation needs to be configured on the class (generally configured in the SpringBoot startup class) to enable economy and annotation caching support.

@EnableCaching //SpringBoot开启缓存注解支持
@SpringBootApplication
public class DemoApplication {
    
    

    public static void main(String[] args) {
    
    
        ConfigurableApplicationContext app = SpringApplication.run(DemoApplication.class, args);
}

2. @Cacheable annotation

The @Cacheable annotation is also provided by the spring framework and can be used on classes or methods (usually used in data query methods), and the results of the other party are cached and stored. The execution order of @Cacheable annotation is advanced cache query. If it is empty, method query is performed and the result is cached. If there is data in the cache, method query is not performed, but the cached data is used directly.

@Cacheable annotation related attributes

  1. value/cacheNames: Specify the name of the cache space, matching attributes. Can choose one to use.
  2. key: Specify the key of the data in the cache. The method parameter value is used by default, and SPEL expressions can also be used.
  3. keyGenerator: Specify the key generator of the cached data, and use one of the key attributes.
  4. cacheManager: Specifies the manager of the cache
  5. cacheResolver: Specify the cache resolver, which can be used with the cacheManager attribute.
  6. condition: Specifies that data is cached under certain conditions.
  7. unless: Specifies not to cache data under certain conditions.
  8. sync: Specify whether to use asynchronous caching, the default is false.

3. @CachePut annotation

The @CachePut annotation is provided by the Spring framework and can be applied to classes or methods (usually used in data update methods). The function of this annotation is to update cached data. The order of execution of the @CachePut annotation is to call the method first, and then update the result of the method to the cache.
The @CachePut annotation also provides multiple attributes, which are exactly the same as those of the @Cacheable annotation.

4. @CacheEvict annotation

The @CacheEvict annotation is provided by the Spring framework and can be applied to classes or methods (usually used in data deletion methods). The function of this annotation is to delete cached data. The default execution order of the @CacheEvict annotation is to call the method first, and then clear the cache.
The @CacheEvict annotation also provides multiple attributes, which are basically the same as those of the @Cacheable annotation. In addition, it also provides two additional special attributes, allEntries and beforeInvocation.

  • allEntries_ attribute: The
    allEntries attribute indicates whether to clear all cache data in the specified cache space, the default value is false (that is, only the cache data corresponding to the specified key is deleted by default). For example, @CacheEvict(cacheNames = "comment" allEntries = true) means that all data in the cache space comment will be deleted after the method is executed.
  • beforeInvocation attribute: The
    beforeInvocation attribute indicates whether to clear the cache before the method is executed, and the default value is false (that is, the cache clears after the method is executed by default). For example, @CacheEvict(cacheNames = "comment", beforeInvocation = true) means that the cache will be cleared before the method is executed.
  • It should be noted that if the beforeInvocation attribute of the @CacheEvict annotation is set to true, there will be certain drawbacks. For example, an exception occurs in the method of data deletion, which will cause the actual data to not be deleted, but the cached data is cleared in advance.

5. @Caching annotation

@Caching annotation is used for data cache management for complex rules. It can be applied to classes or methods. The @Caching annotation contains three attributes: Cacheable, put, and evict, which correspond to the three annotations @Cacheable, @CachePut and @CacheEvict, respectively. , The sample code is as follows.

	@Caching(cacheable={
    
    8cacheable (gacheNames ="comment".,key = "帮id")},
	put = {
    
    RCachePut(cacheNames = "comment".,key.= "#result.author")})
	public Comment getcoiment(int comment id){
    
    
		return commentRepository.fi.ndByld(comment id).get(); 
	}

In the above code, the getComment) method uses the @Caching annotation, and uses the cacheable and put two attributes nested to introduce the @Cacheable and @CachePut two annotations, with id and author as the cache key value, and the query result Comment as Cache value value for cache management.

6. @CacheConfig annotation.

@CacheConfig… Annotation is used on the class, mainly used to manage all public attributes in the class using @Cacheable, @CachePut… and @CacheEvict annotations. These public attributes include cacheNames, keyGenerator, cacheManager and cacheResolver, sample code as follows.

	@CaaheConfig.(caaheNames - "comment")service
		public class CommsntService {
    
    
			@Autowired
			private CommentRepository commentRepository;
			public Comment findById(int comment. id){
    
    
			Comment comment = commentRepository.findById(commenc..id).get ();
			return comment; 
		}
	}

In the above code, the CommentService class is annotated with @CacheConfig annotation, and at the same time, the cache space is set to comment by using the cacheNames. property, so that the corresponding cacheNames property can be omitted when using cache annotations on all methods in this class.
It should be noted that if @CacheConfig is used on the class, the annotation defines a certain attribute (such as cacheNames), and at the same time the cache annotation is used to define the same attribute in the method of this class, then the attribute value will use the "principle of proximity" "The selection is based on the attribute value in the annotation on the method.

Guess you like

Origin blog.csdn.net/weixin_44676935/article/details/109407062