@CacheEvict AllEntries and beforeInvocation
In spring cache, @CacheEvict is an annotation for clearing caches.
The annotation parameter can have only value, and key means to clear the key value data in the value value space. At this time, the default is to clear the current annotation method after it is successfully executed.
There will be a problem at this time. Maybe your annotation method successfully performed the delete operation, but the subsequent code throws an exception and fails to clear the cache. The next time you query, the result value found is the value before the delete operation.
There is a simple solution, adding beforeInvocation to true in the annotation parameter, which means that when the cache is cleared before executing this method, the cache will not exist regardless of whether the method is successful or not.
When the annotation parameter is added to allEntries to true, it means that this clear cache is to clear all cached data under the current value value space.
@CacheEvict annotation parameters detailed explanation
Recently, the two annotations @Cacheable and @CacheEvict have been used in the project to add and clear caches, but sometimes it appears in the same transaction that first makes the cache clear, but a method that is not in this transaction calls the query and puts the data before the update into the cache again, so I want to study the annotation of @CacheEvict cache deletion
A required attribute is used to specify which cache area the method clears data. By default, it is an empty array.
2. allEntries
This property refers to whether the entire cache is clear
This attribute is more critical. It means whether to delete the cache before executing the corresponding method. By default, false (that is, delete the cache after executing the method). When we encounter the need to delete the cache before executing the method, that is, we need to clear the cache regardless of whether the method is executed successfully. Then we can change the value of beforeInvocation to true
This property specifies a SpEL expression, and the cache will be cleared only if the expression is true
Display the specified cached key through SpEL expression
eg: key = "# + ‘all'"
cache name collection, data structure is array type
What I use more is the use of beforeInvocation, because according to different business logic, there will be different cache deletion scenarios. You need to carefully consider whether to set to true or false. The article is relatively shallow, so you can roughly understand the meaning of each parameter.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.