razvan
razvan

Reputation: 589

Spring Boot Multiple Cache Managers in parallel

I have a Spring Boot Web application and using spring session with a redis store. The web requests sometimes need their responses to be cached (to avoid unnecessary db trips) and I planned to use Caffeine.
However it seems Redis takes over (as soon as I include the gradle dependency) as a caching implementation as all my TTL set for Caffeine are ignored.

Is it even possible / recommendable to use more than 1 Cache provider in a Spring Boot application? I could try to use Redis for all the caches, just worry that it will affect the session implementation which comes with Spring Boot (I didn't configure anything there just used @EnableRedisHttpSession).

I appreciate any advice on this.

Upvotes: 1

Views: 2889

Answers (1)

jihor
jihor

Reputation: 2599

You can use separate cache managers with @Cacheable:

@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
@Cacheable(key = "#name", cacheManager = "caffeineCacheManager")
public String greeet(@PathVariable String name) {
    return "Hello " + name;
}

and the only thing you need is to have your cache manager as a named bean:

@Bean
@Qualifier("caffeineCacheManager")
AbstractCacheManager caffeineCacheManager() {
    return new CaffeineCacheManager();
}

Upvotes: 3

Related Questions