Is there something along the strains of @CacheEvict(value = "FOO", key = "baz*") so that when the cache FOO contains keys baz_1 and baz_2 they get uprooted?
1 Replies
Assuming that you have spring-boot-starter-cache as a dependency, spring boot auto-configures a CacheManager bean named cache manager.
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 implementations.
The shortcoming of this approach is that you'll have to change your code upon changing the cache implementation.