Spring cache annotations @cache, @CachePut, @CacheEvict, @CacheConfig using

Source: Internet
Author: User

@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 td> whether to empty all cached content, defaults to False, and if true, clears all caches immediately after the method call
@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 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.