Home > Enterprise >  Which Cache Manager/Cache library to use for Spring and EHCache 3?
Which Cache Manager/Cache library to use for Spring and EHCache 3?

Time:07-05

Integrating EhCache3 cache provider for spring boot app. I need to decide which cache manager to use. Ideally I want to use Springs caching annotations on my caching methods such as @Cacheable instead of the jsr (@CacheResult) but for cachemanager/cache libraries I cant decide on the below

Cache library annotations Im deciding on with ehcache3 provider :

javax ehcache spring Jcache?(which I thought was jsr/javax)
javax.cache.Cache org.ehcache.Cache org.springframework.cache.Cache org.springframework.cache.Cache
javax.cache.Cache.CacheManager org.ehcache.CacheManager org.springframework.cache.CacheManager org.springframework.cache.jcache.JCacheCacheManager

Any suggestions on which implementation? Maybe I am not to clear on what differentiates the above implementations and when to choose one over the other

CodePudding user response:

ehcache is pretty old and well known caching library. Its also the default 2nd level cache in Hibernate. I have used ehcache in many many applications without any issues , so I would go with ehcache.

CodePudding user response:

The short answer:

EHCache3 does not have an extra Spring integration but is integrated via the general Java caching API JCache/JSR107 integration. The relevant official documentation can be found at: https://docs.spring.io/spring-framework/docs/5.3.x/reference/html/integration.html#cache-store-configuration-ehcache

Longer answer:

The options you present are not real options but different concepts. I try to explain it briefly:

  • javax.cache.Cache: This is the interface that a JSR107 compliant cache implements. EHCache3 also provides an implementation of this interface.
  • org.ehcache.Cache: That is the native interface of a EHCache3 cache that may have different functions not supported by the JSR107 Cache interface. If only standard Spring is used in your application, except for the configuration which needs to decide which cache cache implementation to use, you will not get in contact with this interface.
  • org.springframework.cache.Cache: That is the interface of the abstracted Spring cache. It only has a very small subset of cache operations that are needed for Spring. The Spring Caching is always using this interface.
  • org.springframework.cache.jcache.JCacheCacheManager: That is an implementation of org.springframework.cache.CacheManager usable JCache compliant caches. That will be used with EHCache3 as well since EHCache3 is integrated via JSR107.

So when using a JSR107 compliant cache there are two API translations: The Spring cache implementation translates to a JSR107 cache and than the JSR107 cache implementation translates to the native cache API.

  • Related