[SpringBoot] Cache-related comments

1. @EnableCaching: Mainly used to enable annotation-based caching support, used in the Application class

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

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

2. @CacheConfig: This annotation is added to the class to extract the public configuration of the cache.

Adding the @CacheConfig annotation to the class header is equivalent to adding the component specified by cacherName or value to the cache annotation on each method , and this component comes from @CacheConfig, which is used to coordinate all management classes that use @Cacheable and @CachePut ...and the public properties in the @CacheEvict annotation annotation method , these public properties include cacheNames, keyGenerator, cacheManager and cacheResolver

  @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 @CacheConfig annotation is marked on the CommentService class, and the cacheNames. attribute is used to set the cache space to comment, so that the corresponding cacheNames attribute 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 property (such as cacheNames), and at the same time, the same property is defined in the class method using the cache annotation, then the property value will use the "proximity principle "Selection is subject to the attribute value in the annotation on the method.

3. @CachePut: This annotation is used to set the cache

Indicates that the cache is updated while calling the method. It calls the target method first by default, and then stores the running result of the target method in the cache. However, it should be noted that if you want to keep the cache updated synchronously, the key used in this annotation needs to be the same as the cache. The key remains the same.
The difference from @cacheable is that @cacheable runs before the target method, but after the target method, because it needs to get the result of the target method first. @Cacheable cannot use #result, because the method marked with @Cacheable may not be called, and the result may not be obtained. Its attributes are basically the same as @cacheable, except for an attribute of whether it is asynchronous (sync)

@CachePut(value="dep",key="#department.id")
    public Department updateDepartment(Department department){
    
    
       System.out.println("更新信息:"+department);
       departmentDao.updateDepartment(department);
       return department;
    }

4. @Cacheable(value=”accountCache”): This annotation is mainly for method configuration, which can cache the results according to the request parameters of the method. For example, if the value exists in the cache, the cached data will be used. If it is not in the cache, then stored in the cache;

This comment means that when this method is called, it will be queried from a cache named accountCache. If not, the actual method (that is, querying the database) will be executed, and the execution result will be stored in the cache, otherwise it will be returned objects in the cache . The key in the cache here is the parameter userName, and the value is the Account object. The "accountCache" cache is the name defined in spring*.xml.
Attributes:

  1. value/cacheNames: Specify the name of the cache space and match the attributes. You can choose one of the two.
  2. key: Specifies 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: Specifies the generator for the key of the cached data, which can be used with the key attribute.
  4. cacheManager: Specifies the cache manager
  5. cacheResolver: Specifies the cache resolver, which can be used with the cacheManager attribute.
  6. condition: Specifies that data caching should be performed when a certain condition is met.
  7. unless: Specifies not to cache data when a certain condition is met.
  8. sync: Specifies whether to use asynchronous caching, the default is false.

5. @CacheEvict: This annotation is used to clean up the cache

Comment to mark the method to clear the cache, first perform the method call, and then clear the cache.
Pay attention to one of @CacheEvict(value=”accountCache”, key=”#account.getName()”), the Key in which is used to specify the key of the cache, because we use the name field of the account object when saving , so it is also necessary to obtain the value of name from the parameter account object as the key. The # in front means that this is a SpEL expression, and this expression can traverse the parameter object of the method.
This annotation provides two special attributes:
allEntries attribute: Indicates whether to clear all key-value pairs in the specified cache, that is, whether to clear all caches. When set to true, all key-value pairs in the cache will be cleared. The default is false, that is, according to key to clear the cache. So it can be used with the key attribute.
beforeInvocation attribute: Indicates whether to clear the specified cache before the @CacheEvict-annotated method call. The default is false, that is, the cache is cleared after the method call. When set to true, the cache will be cleared before the method call. The difference between clearing the cache before or after the method call is whether there will be an exception when the method is called. If there is no exception, there is no difference between the two settings. If an exception occurs, setting it to clear the cache after the method call will not work, because the method The call failed.

@CacheEvict(value = "dep",key = "#id"/*,beforeInvocation = true*//*,allEntries = true*/)
    public boolean delDep(int id){
    
    
        boolean flag=false;
        try{
    
    
            departmentDao.deleteDepartmentById(id);
            flag=true;
        }catch(Exception e){
    
    
            e.printStackTrace();
        }
        return flag;
}

6. @Caching: This annotation can package cache cleaning, setting and other operations

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

@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 two annotations @Cacheable and @CachePut are nested using the cacheable and put attributes. The id and author are respectively used as the cache key value, and the query result Comment is used as the cache value for cache management.

Guess you like

Origin blog.csdn.net/m0_46459413/article/details/129934944