EhCache 3 and Spring Boot

Christina picture Christina · Feb 4, 2018 · Viewed 8.5k times · Source

Is there a way to abstract the EhCache 3 CacheManager (org.ehcache.CacheManager) to Spring's CacheManager (org.springframework.cache.CacheManager)?

With EhCache2, it is possible, by the following:

  @Bean
  public org.springframework.cache.CacheManager cacheManager(net.sf.ehcache.CacheManager ehcache) {    
    EhCacheCacheManager cacheManager = new EhCacheCacheManager(ehcache);
    return cacheManager;
  }

HINT: I've found a way to abstract the javax.cache.CacheManager cacheManager to the Spring's CacheManager (org.springframework.cache.CacheManager), by:

  @Bean
  public org.springframework.cache.CacheManager cacheManager(javax.cache.CacheManager cacheManager) {
    return new JCacheCacheManager(cacheManager);
  }

It will be also helpful if there is a way to cast org.ehcache.CacheManager to javax.cache.CacheManager .

Thanks.

Answer

Anthony Dahanne picture Anthony Dahanne · Feb 5, 2018

Yes, you should rely on the standard Java caching specification, JSR-107, aka JCache (javax.cache.CacheManager)

Spring has a nice integration of it, and Ehcache2 and Ehcache3 also are compatible with it

Please have a look at this answer, it also comes with some simple examples : https://stackoverflow.com/a/39340151/24069