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

What is the difference between allEntries and beforeInvocation in @ CacheEvict

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

Share

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

This article will explain in detail what is the difference between allEntries and beforeInvocation in @ CacheEvict. 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.

The difference between @ CacheEvict allEntries and beforeInvocation

In spring cache, @ CacheEvict is a comment that clears the cache.

The annotation parameter can only mean to clear the key data in the value value space, which is cleared by default after the current annotation method is successfully executed.

At this time, there will be a problem. Maybe your annotation method successfully performs the delete operation, but the subsequent code throws an exception and fails to clear the cache, and the next query will still read it from the cache. In this case, the result value of the query is the value before the delete operation.

A simple solution is to add beforeInvocation to the annotation parameters as true, which means that the cache will not exist regardless of whether the method is executed successfully or not.

When the annotation parameter is added with allEntries as true, it means that the purge cache clears all cached data under the current value space.

Detailed explanation of @ CacheEvict annotation parameters

Recently, @ Cacheable and @ CacheEvict annotations have been used in the project to add and clear the cache, but sometimes they appear in the same transaction. First, the cache is clear, but a method that is not in this transaction calls the query and puts the data before the update back into the cache, so I want to study the annotation of @ CacheEvict cache deletion.

1.value

Must be an attribute that specifies which cache the method clears. The default is an empty array.

2. AllEntries

This attribute refers to whether the entire cache is clear.

3.beforeInvocation

This attribute is critical. What he means is whether to delete the cache before the corresponding method is executed. By default, false (that is, delete the cache after the method is executed). When we encounter the need to delete the cache before the method is executed, that is, whether the method is executed successfully or not, we need to clear the cache, then we can change the value of beforeInvocation to true.

4.condition

This property specifies a SpEL expression that clears the cache only if the expression is true

5.key

Displays the specified cached key through an SpEL expression

Eg: key = "# warehousePo.guid + 'all'"

6.cacheNames

A collection of cache names. The data structure is of array type.

What I use more is the use of beforeInvocation, because according to different business logic, cache deletion will occur in different scenarios, so you need to carefully consider whether to set it to true or false. The article is relatively shallow, so you can roughly understand the meaning of each parameter.

So much for sharing the difference between allEntries and beforeInvocation in @ CacheEvict. I hope the above content can be helpful to you 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