Spring Cache Annotations Detailed

Source: Internet
Author: User

@CacheConfig: Used primarily to configure some common cache configurations that are used in this class. Here @cacheconfig (cachenames = "users"): The content returned in this data Access object is configured to be stored in a cache object named users, and we can also define it directly by @cacheable the name of the cache set that you configure by itself, without using the annotation.

@Cacheable: The return value configured with the Findbyname function is added to the cache. At the same time, the query is fetched from the cache, and if it does not exist, then access to the database is initiated. The note has the following main parameters:

@Cacheable (cachenames= "Books", key= "#isbn") Public Book Findbook (ISBN isbn, Boolean checkwarehouse, Boolean includeused ) @Cacheable (cachenames= "Books", key= "#isbn. Rawnumber") Public Book Findbook (ISBN isbn, Boolean checkwarehouse, Boolean includeused) @Cacheable (cachenames= "Books", key= "T (SomeType). Hash (#isbn)") Public Book Findbook (ISBN ISBN, Boolean Checkwarehouse, Boolean includeused) @Cacheable (cachenames= "Books", key= "#map [' BookID '].tostring ()") Public book Findbook (map<string, object> Map)

Cached sync Sync:
In a multithreaded environment, some operations may be called synchronously using the same parameters. By default, the cache does not lock any resources, which can result in multiple computations that violate the purpose of the cache. For these specific situations, attribute sync can indicate that the underlying cache is locked so that only one thread can enter the calculation, while the other threads are blocked until the returned results are updated into the cache.
Cases:

@Cacheable (cachenames= "Foos", sync= "true") public Foo executeexpensiveoperation (String ID) {...}

Value, Cachenames: two equivalent parameters (Cachenames is new for Spring 4, as an alias for value), specifying the collection name of the cache store. Because of the new @cacheconfig in spring 4, the Value property that was required in Spring 3 is also not required.

Key: The cache object stored in the map collection of the key value, not required, by default all parameters of the function as the key value, if you configure the use of Spel expression, such as: @Cacheable (key = "#p0"): Using the function of the first parameter as the key value of the cache, More details on Spel expressions can be found in the official documentation

Condition: The condition of the cached object, not required, also requires the use of the Spel expression, only content that satisfies the expression condition is cached, such as: @Cacheable (key = "#p0", condition = "#p0. Length () < 3"), Indicates that the first parameter will be cached only if it is less than 3, and if the AAA user above this configuration is not cached, the reader can try it on its own.

@Cacheable (cachenames= "book", condition= "#name. Length < +") public book Findbook (String name)

Unless: Another cache condition parameter, not required, requires the use of a spel expression. It differs from the condition parameter where it is judged by its timing, which is judged after the function is called, so it can be judged by the result.

@Cacheable (cachenames= "book", condition= "#name. Length <", unless= "#result. Name.length > 5") Findbook (String name)

Keygenerator: Used to specify the key generator, not required. If you need to specify a custom key generator, we need to implement the Org.springframework.cache.interceptor.KeyGenerator interface and use this parameter to specify. It is important to note that this parameter is mutually exclusive to key

CacheManager: Used to specify which cache manager to use, not required. Only need to use when there are multiple

Cacheresolver: Used to specify that the cache parser is used, not required. The Org.springframework.cache.interceptor.CacheResolver interface is required to implement its own cache parser, which is specified with this parameter.

In addition to the two annotations used here, there are several core annotations:

@CachePut: Configured on the function, can be cached according to the parameters of the definition of the condition, it is different from @cacheable, it is really called function every time, so it is mainly used for data addition and modification operations. Its parameters are similar to @cacheable, the specific function can refer to the analysis of the @cacheable parameters on the face

@CachePut (cachenames= "book", Key= "#isbn") Public Book Updatebook (ISBN ISBN, Bookdescriptor descriptor)

@CacheEvict: Configured on a function, typically used on a delete method, to remove the data from the cache. In addition to the same parameters as @cacheable, it has the following two parameters:

Allentries: Not required, default is False. When True, all data is removed

@CacheEvict (cachenames= "books", allentries=true) public void Loadbooks (InputStream batch)

Beforeinvocation: Not required, default is False, the data is removed after the method is called. When True, data is removed before the method is called.

@CacheEvict (cachenames= "books", beforeinvocation=true) public void Loadbooks (InputStream batch)

Spring Cache Annotations Detailed

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.