Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ CacheEvict to clear all caches specified

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use @ CacheEvict to clear all the caches specified. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

@ CacheEvict clears all caches @ CacheEvict under the specified (cacheNames = "parts:grid", allEntries = true)

This comment clears all caches under part:grid

@ CacheEvict requires that one or more caches be specified so that all of them are affected. In addition, an additional parameter, allEntries, is provided. Indicates whether all elements in the cache need to be cleared. The default is false, which means it is not needed.

When allEntries is specified as true, Spring Cache ignores the specified key. Sometimes we need to Cache all the elements at once.

@ Cacheable cache @ CachePut: cache update @ CacheEvict: cache deletion @ Cacheable cache

Note: in an environment that supports Spring Cache, for the method marked with @ Cacheable, Spring checks whether the cache element of the same key exists in the Cache before each execution. If so, the method is no longer executed, but the result is directly obtained from the cache and returned, otherwise it will be executed and the returned result will be stored in the specified cache.

/ / @ since 3.1 can be marked on methods and classes as @ Target ({ElementType.METHOD, ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Inherited@Documentedpublic @ interface Cacheable {/ / Cache names can be written multiple, the real composition of key, prefixed with cacheName, multiple key will generate @ AliasFor ("cacheNames") String [] value () default {}; @ AliasFor ("value") String [] cacheNames () default {} / / support writing SpEL, you can use # root,# parameter name "or" # p parameter index "/ / details to https://blog.csdn.net/dalong_bamboo/article/details/103844076 String key () default"; / / Mutually exclusive: it and the key attribute are mutually exclusive. Please use only one, and you can String keyGenerator () default ""; / / to choose which cacheManager String cacheManager () default "" to use; / / to define how to handle the cache and implement the org.springframework.cache.interceptor.CacheResolver interface String cacheResolver () default ""; / / to indicate in which case the results will be cached. You can use SpEL or # root. Only when true is used, String condition () default ""; / / indicates in which case the result is not cached. You can write SpEL # root, and you can use # result to get the classic value of the method # result = = null String unless () default ""; / / true: force synchronous execution. (if multiple threads try to load a value for * * the same key * * and call the target method synchronously) / / the advantage of synchronization is that the latter thread will read the cached data of the previous cache, so there is no need to check the library ~ ~ / / false by default Note that sync rather than Async / / its resolution depends on the Cache.get (Object key, Callable valueLoader) provided by Spring4.3. Method boolean sync () default false;} @ CachePut: cache update

Note: @ CachePut can also declare that a method supports caching.

Unlike @ Cacheable, the method using the @ CachePut annotation does not check whether there is a previously executed result in the cache before execution, but executes the method each time and stores the result in the specified cache in the form of a key-value pair.

@ Target ({ElementType.METHOD, ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Inherited@Documentedpublic @ interface Cacheable {@ AliasFor ("cacheNames") String [] value () default {}; @ AliasFor ("value") String [] cacheNames () default {}; / / Note: it is different from the above. Here key it can also use # result String key () default ""; String keyGenerator () default ""; String cacheManager () default ""; String cacheResolver () default ""; String condition () default ""; String unless () default "";} @ CacheEvict: cache deletion

Note: @ CacheEvict is used to mark methods or classes that need to clear cached elements. When marked on a class, it means that the execution of all methods in it triggers a cache cleanup operation.

The properties that @ CacheEvict can specify are value, key, condition, allEntries, and beforeInvocation. The semantics of value, key, and condition are similar to the corresponding attributes of @ Cacheable.

That is, value indicates the Cache on which the cleanup operation occurred (corresponding to the name of the Cache); key indicates which key needs to be cleared, and if not specified, the key;condition generated by the default policy is used to indicate the conditions under which the purge operation occurs.

Let's take a look at the two new properties allEntries and beforeInvocation.

The allEntries attribute, where allEntries is of type boolean, indicates whether all elements in the cache need to be cleared.

The default is false, which means it is not needed. When allEntries is specified as true, Spring Cache ignores the specified key.

Sometimes we need to Cache all the elements at once, which is more efficient than clearing elements one by one.

@ Target ({ElementType.METHOD, ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Inherited@Documentedpublic @ interface Cacheable {@ AliasFor ("cacheNames") String [] value () default {}; @ AliasFor ("value") String [] cacheNames () default {}; / / it can also use # result String key () default "; String keyGenerator () default"; String cacheManager () default ""; String cacheResolver () default ""; String condition () default "" / / whether to clear all the caches specified by the above cacheNames. The default is false boolean allEntries () default false; / / whether to allow the cache cleanup action to be performed before the target method. The default is false (after the target method) / / Note: if executed later, once the target method throws an exception, the cache will not be cleared ~ boolean beforeInvocation () default false } this is all about how to use @ CacheEvict to clear all the caches specified. I hope the above content can be of some help and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report