Techiio-author
Started by Bell GrantNov 5, 2021

Open
Is it possible to @CacheEvict keys that match a pattern?

1 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWES SHARE

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

Techiio-commentatorHarry Lian replied 3 months ago0 likes0 dislikes

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.

You must be Logged in to reply
Trending Technologies
15
Software40
DevOps47
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance