@Cacheable, @CachePut, @CacheEvict notes are described in Table 1. @Cacheable function and Configuration method
The role of @Cacheable is primarily for method configuration, which caches the results based on the request parameters of the method.
@Cacheable the main parameters |
Value |
The name of the cache, as defined in the spring configuration file, must specify at least one |
For example: @Cacheable (value= "Mycache") or @Cacheable (value={"Cache1", "Cache2"} |
Key |
The cached key, which can be null if specified to be written according to the SPEL expression, is combined by default by all parameters of the method if not specified |
For example: @Cacheable (value= "Testcache", key= "#userName") |
Condition |
Cached condition, can be empty, written with Spel, returns TRUE or FALSE, only true for caching |
For example: @Cacheable (value= "Testcache", condition= "#userName. Length () >2") |
--------------------------------------------------------------///////////////////////////////////////////////// Table 2. @CachePut function and Configuration method
@CachePut's role is mainly for the method configuration, can be based on the method's request parameters to cache its results, and @Cacheable is different, it will trigger the real method of the call each time
@CachePut the main parameters |
Value |
The name of the cache, as defined in the spring configuration file, must specify at least one |
For example: @Cacheable (value= "Mycache") or @Cacheable (value={"Cache1", "Cache2"} |
Key |
The cached key, which can be null if specified to be written according to the SPEL expression, is combined by default by all parameters of the method if not specified |
For example: @Cacheable (value= "Testcache", key= "#userName") |
Condition |
Cached condition, can be empty, written with Spel, returns TRUE or FALSE, only true for caching |
For example: @Cacheable (value= "Testcache", condition= "#userName. Length () >2")
|
Table 3. @CacheEvict function and Configuration method
The role of @CachEvict is mainly for the method configuration, to be able to empty the cache according to certain conditions
cache key, which can be empty, if specified to be written in spel expression, if not specified, is combined by default by all parameters of the method
@CacheEvict main parameters |
value |
The name of the cache, defined in the spring configuration file, must specify at least one |
For example: @CachEvict (value= "Mycache") or @CachEvict (value={"Cache1", "Cache2"} |
key |
for example: @CachEvict (value= "Testcache", key= "# UserName ") |
condition |
Cached condition, can be empty, write with Spel, return True or FALSE, only true to empty cache |
Example such as: @CachEvict (value= "Testcache", condition= "#userName. Length () >2") |
allentries | td> whether to empty all cached content, defaults to False, and if true, clears all caches immediately after the method call
For example: @CachEvict (value= "Testcache", Allentries=true) |
beforeinvocation |
is emptied before the method executes, defaults to False, and if specified as true, empties the cache when the method has not been executed, by default, if the method does throw an exception, Does not empty the cache |
for example: @CachEvict (value= "Testcache", beforeinvocation=true) |
additional supplement : @cache ("something"), this equivalent to the Save () operation, @cachePut equivalent to the update () operation, as long as the method he marked is called, then it will be cached. The @cache is the first to see if there is a cache, and then choose whether to execute the method. @CacheEvict equivalent to the delete () operation. Used to clear the cache.
Spring cache annotations @cache, @CachePut, @CacheEvict, @CacheConfig using