Sha
Sha

Reputation: 1181

Multiple Cache Layer in Spring Boot with Eviction Policy

I want to use spring Cacheable effectively. For example, my dream is to use it with multiple-layer like EhCache + Redis together. https://medium.com/@estebanbett/using-ehcache-and-redis-as-multiple-cache-managers-in-springboot-4e6f37b0f94

I found some solutions to use multiple cache managers together. But I need a robust eviction policy also. For example, for scalable system, when we evict a key, I need to evict this key from ehcache and redis-cache together on all instances. Is it possible?

  @Bean
  public JCacheCacheManager jCacheCacheManager() {
    return new JCacheCacheManager(ehCacheManager());
  }

  @Bean
  public CacheResolver cacheResolver() {
    return new CustomCacheResolver(jCacheCacheManager(), redisCacheManager());
  }

  @CacheableConfig(cacheTarget = CacheTarget.MEMORY_AND_SHARED)
  @Cacheable(cacheNames = {"mathCache"}, cacheResolver = "cacheResolver")
  public Integer substract(int a, int b) {
    return a - b;
  }

what about eviction policy on multiple cache manager?

@CacheEvict(value = "...", allEntries = true) -> when we evict it, evict from all instances that include redis + ehcache.

Upvotes: 0

Views: 97

Answers (0)

Related Questions