Is there something along the lines of @CacheEvict(value = "FOO", key = "baz*")
so that when the cache FOO
contains keys baz_1
and baz_2
they get evicted?
Asked
Active
Viewed 1,698 times
1

Peter Catalin
- 1,342
- 1
- 14
- 22
1 Answers
1
Assuming that you have spring-boot-starter-cache
as dependency, spring boot auto-configures a CacheManager
bean named cacheManager
.
Also, assuming you have spring-boot-starter-data-redis
as a dependency, RedisCacheManager
is picked as the CacheManager
implementation.
@CacheEvict
(and the caching abstraction API) doesn't let you the option to evict by prefix, but using an AOP advice (or elsewhere where fits), you can take advantage of the underlying implementation:
RedisCache redisCache = (RedisCache) cacheManager.getCache("FOO");
redisCache.getNativeCache().clean("FOO", "baz*".getBytes());
Didn't try it actually, but I think this should work.
Likewise, you can adapt to other caching implementation.
The shortcoming of this approach, is that you'll have to change your code upon changing the cache implementation.

Ori Dar
- 18,687
- 5
- 58
- 72
-
Your assumptions were right. It worked. Thank you! :) – Peter Catalin Nov 05 '21 at 15:00