In my application I have multiple cacheable methods with multiple keys:
@Cacheable(cacheNames = "valueCodes", key = "{#value, #fieldId, #projectId}")
@Cacheable(cacheNames = "fieldNames", key = "{#field, #value, #projectId}")
@Cacheable(cacheNames = "qi", key = "{#langCode, #question, #projectId}")
@Cacheable(cacheNames = "fieldCodes", key = "{#name, #projectId}")
Now I want a cachevict method which cleares all the caches where only the #projectId key, which is a UUID, matches:
@CacheEvict(value = {"valueCodes", "fieldCodes", "qi"; "fieldCodes"}, key = "#projectId")
I've read in this article that this is not possible and that
Only the evict annotation's key regex matching more than one element in each of the cacheNames
I'm not really sure what they mean by that, but I guess it has something to do with using regex in SpEL.
So I started thinking about concatinating my keys into one key:
@Cacheable(cacheNames="cahceName", key="concat(#projectId).concat(#otherKey)")
and using regex to match all keys with the projectId followed by a wildcard. But I couldn't really find a way to do this.
Is what I'm trying to accomplish possible? If so, how do I do this?
Is what I'm trying to accomplish possible? If so, how do I do this?
What you like to do is not possible.
In general a cache acts like a hash table, you can only operate on a unique key. Selecting everything which belongs to a project id would require an index and a query mechanism in the cache. Some caches have that, but not all, and there is no common standard how this is done.
Double check whether it really makes sense to cache all the bits and pieces that belong to a project separately. If everything needs to be evicted together, maybe it is used together all the time. Alternatively, for example, keep a ConcurrentHashMap
as value in the cache which holds the various components belonging to a project.
More on this, see the question: What is the better option for a multi-level, in-process cache?
Probably it makes sense to drop annotations and use a cache directly. The options with annotations are limited.