1

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.

razvan
  • 559
  • 7
  • 23
  • 1
    There is a [detection order](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html#_supported_cache_providers). My understanding that Spring Cache was designed to allow for multiple cache managers and doing so is considered okay. – Ben Manes Jun 16 '17 at 05:19

1 Answers1

2

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();
}
jihor
  • 2,478
  • 14
  • 28