Caheput,Cacheable,CacheEvict

        This article only introduces theory

        Theoretical reference  SpringBoot's cache implementation Cache and custom cache manager

        Detailed usage reference

        spring cache learning - detailed explanation of @Cacheable usage

        Instructions for using the @CacheEvict annotation of SpringCache

        spring cache learning - detailed explanation of @CachePut usage

Cacheable

        Running process:

        1. Before running the method, first query the Cache (cache component) and obtain it according to the name specified by cacheNames; (CacheManager obtains the corresponding cache first). If the cache is obtained for the first time, it will be automatically created if there is no Cache component.

        2. Find the cached content in the Cache and use a key. The default is the parameter of the method; the key is generated according to a certain strategy; the default is to use keyGenerator to generate the key. The default is to use SimpleKeyGenerator to generate the key; the default strategy for SimpleKeyGenerator to generate the key; if No parameters; key=new SimpleKey(); If there is one parameter: key=the value of the parameter If there are multiple parameters: key=new SimpleKey(params);

        3. Call the target method without finding the cache;

        4. Put the result returned by the target method into the cache. Before executing the method marked with @Cacheable, check whether there is such data in the cache. By default, the cache will be queried according to the value of the parameter as the key. If not, the method will be run and the result will be put into the cache. into the cache; you can directly use the data in the cache when you call it later;

Cacheput

        Updating the cache is equivalent to querying the database and putting the results into the cache, thereby updating the data in the cache.

CacheEvict

        CacheEvict is an annotation in the Spring framework, which can be used to clear the cache. When we use CacheEvict annotation, it clears the cache before or after method execution. The CacheEvict annotation has some attributes that can be used to specify the cache name, cache key, etc. to be cleared.

        The principle of CacheEvict is very simple. When we use CacheEvict annotation, it clears the cache before or after method execution. Specifically, it will call the evict method of the cache manager before or after the method is executed to clear the specified cache. The cache manager finds the cache to clear based on the cache name and cache key, and then clears it.
        Using the CacheEvict annotation can bring many benefits. First, it can help us solve the expiration problem of cached data. When cached data expires, we can use the CacheEvict annotation to clear the cache to ensure timely updating of cached data. Secondly, it can help us solve the inconsistency problem of cached data. When we modify the data in the database, we can use the CacheEvict annotation to clear the cache to ensure the consistency of the cached data.
        CacheEvict is a very effective way to clear cache. It can help us solve problems such as expiration and inconsistency of cached data, thereby improving application performance and reliability. If you are developing an application that needs to use cache, then I recommend that you use the CacheEvict annotation to clear the cache. It will make your application more stable and reliable.

        

Guess you like

Origin blog.csdn.net/Deikey/article/details/131158086