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

Source: Internet
Author: User
Introduction to @Cacheable, @CachePut, @CacheEvict notes table 1. @Cacheable functions and Configuration methodsThe 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 functions and Configuration methods@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 functions and Configuration methodsThe role of @CachEvict is mainly for the method configuration, to be able to empty the cache according to certain conditions
@CacheEvict the main parameters
Value The name of the cache, as defined in the spring configuration file, must specify at least one For example:
@CachEvict (value= "Mycache") or
@CachEvict (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:
@CachEvict (value= "Testcache", key= "#userName")
Condition Cached condition, can be empty, written with Spel, returns TRUE or FALSE, only true to empty cache For example:
@CachEvict (value= "Testcache",
Condition= "#userName. Length () >2")
Allentries Whether to empty all cached content, default to False, and if true, all caches are emptied immediately after the method call For example:
@CachEvict (value= "Testcache", Allentries=true)
Beforeinvocation Whether to empty before the method executes, the default is False, if specified as true, the cache is emptied when the method has not been executed, by default, if the method does throw an exception, the cache is not emptied For example:
@CachEvict (value= "Testcache", Beforeinvocation=true)

-------------- Additional Supplements: @cache ("something"), this equivalent to the Save () operation, @cachePut equivalent to the update () operation, as long as the method he marked is called, it will be cached, and @cache is 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.
This write configuration of the declaration needs to be configured @enablecache to use, specific configuration can read this article http://blog.csdn.net/sanjay_f/article/details/47363845
If you forget how spel is used, do yourself a favor and read Chapter 9, Spring Expression Language (spel):
------------- [HTML]View plain copy importorg.springframework.stereotype.Service;   importcom.springcache.annotation.Cacheable; @Service @Cacheable public class memcachedservice{@Cacheable (name= "remote", key= "' user_name_ ' + #args [0]") public   String Storeusername (string accountid,string name) {return name;     } @Cacheable (name= "remote") public String storeuseraddress (String accountid,string address) {return address; }   }




Do not know that you notice a problem, is all the @cacheable () there is a name= "xxx" property, which obviously if the method is more, write it is also very tired, if you can declare a one-time, it is convenient, so, with the @cacheconfig this configuration, @ CacheConfig is a class-level annotation This allows to share the cache names, but don't worry, if you write another name in your method, then the name of the method will prevail.
[HTML]View Plain copy @CacheConfig ("Books") public class Bookrepositoryimpl implements Bookrepository {@Cacheable   Public Book Findbook (ISBN ISBN) {...} }

Of course there is another case, @ cacheable (name = "Remote", key = "' user_name_ ' + #args [0]", conditional= "xxx", Allentries=true,beforeinvoca Tion=true), the configuration like this is very long,
<em><span class= "hl-annotation" style= "Color:gray;" ><span class= "Crayon-sy" style= "Font-family:inherit;color: #333333; white-space:pre; Height:inherit; Font-size:inherit!important; Font-weight:inherit!important; Line-height:inherit!important; " >@</span><span class= "crayon-e" style= "Font-family:inherit;color: #04ed0; white-space:pre; Height:inherit; Font-size:inherit!important; Font-weight:inherit!important; Line-height:inherit!important; " >cacheable</span><span class= "Crayon-sy" style= "Font-family:inherit;color: #333333; white-space:pre; Height:inherit; Font-size:inherit!important; Font-weight:inherit!important; Line-height:inherit!important; " > (</span><span class= "crayon-v" style= "Font-family:inherit;color: #02d7a; white-space:pre; height: Inherit Font-size:inherit!important; Font-weight:inherit!important; Line-height:inherit!important; " >name</span><span class= "crayon-h" style= "Font-family:inherit;color: #06fe0; white-space:pre; Height: Inherit; Font-size:inherit!important; Font-weight:inherit!important; Line-height:inherit!important; " > </span><span class= "crayon-o" style= "Font-family:inherit;color: #06fe0; white-space:pre; Height:inherit; Font-size:inherit!important; Font-weight:inherit!important; Line-height:inherit!important; " >=</span><span class= "crayon-h" style= "Font-family:inherit;color: #06fe0; white-space:pre; Height:inherit; Font-size:inherit!important; Font-weight:inherit!important; Line-height:inherit!important; " > </span><span class= "crayon-s" style= "Font-family:inherit;color: #0800; white-space:pre; Height:inherit; Font-size:inherit!important; Font-weight:inherit!important; Line-height:inherit!important; " > "book" </span><span class= "Crayon-sy" style= "Font-family:inherit;color: #333333; white-space:pre; Height:inherit; Font-size:inherit!important; Font-weight:inherit!important; Line-height:inherit!important; " >,</span></span></em><span style= "Color:gray;line-height:1.4; Font-weight:inherit "><em><span class=" hl-annotation "> key=" #isbn "</span></em></ Span><span class= "crayon-h" style= "Font-family:inherit;color: #06fe0; font-style:italic; Height:inherit; Font-size:inherit!important; Line-height:inherit!important; Font-weight:inherit!important; " >,conditional= "xxx", <span style= "color: #555555; font-size:13px; line-height:15.3599996566772px ">allentries=true,<span style=" line-height:15.3599996566772px "> Beforeinvocation=true</span></span></span><span class= "Crayon-sy" style= "font-family: Inherit;font-style:italic; Height:inherit; Font-size:inherit!important; Line-height:inherit!important; Font-weight:inherit!important ">)  </span><em><span class=" hl-annotation "style=" Color:gray ;" ><span class= "crayon-h" style= "Font-family:inherit;color: #06fe0; white-space:pre; Height:inherit; Font-size:inherit!important; Font-weight:inherit!important; Line-heigHt:inherit!important; " > </span></span></em><span class= "hl-keyword" style= "color: #7f055; Font-weight:bold" > Public</span> Book Findbook (ISBN ISBN, <span class= "Hl-keyword" style= "color: #7f055; Font-weight:bold" > Boolean</span> Checkwarehouse, <span class= "Hl-keyword" style= "color: #7f055; font-weight:bold" >boolean </span> includeused)
This configuration is long and may be declared in a number of ways, so we would like to simplify the point, easy to configure. So

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.