Reputation: 6962
We have a new project and I am starting on a part where we need to implement a caching mechanism. After doing some research JCache seemed to be the obvious answer, yet Spring also has a caching mechanism. We are using Spring (and that surely won't change) so the dependency issue could be forgiven if it ensured a better caching mechanism.
So my question is, what would be the pro's and con's of each one? Is JCache quite feature limited compared to Spring's implementation? Or is JCache now considered the way going forward for new projects?
Upvotes: 4
Views: 5371
Reputation: 12716
It's not a 'vs' thing. It's a 'and' thing. From the manual -
Just like other services in the Spring Framework, the caching service is an abstraction (not a cache implementation) and requires the use of an actual storage to store the cache data - that is, the abstraction frees the developer from having to write the caching logic but does not provide the actual stores. This abstraction is materialized by the org.springframework.cache.Cache and org.springframework.cache.CacheManager interfaces.
There are a few implementations of that abstraction available out of the box: JDK java.util.concurrent.ConcurrentMap based caches, Ehcache 2.x, Gemfire cache, Caffeine, Guava caches and JSR-107 compliant caches (e.g. Ehcache 3.x). See Section 36.7, “Plugging-in different back-end caches” for more information on plugging in other cache stores/providers.
As an example I use RedisCacheManager for HTTP sessions.
@Bean
public CacheManager cacheManager() {
Map<String, Long> cacheExpiration = new HashMap<>();
cacheExpiration.put(CACHE_RECOMMENDATION, ONE_HOUR);
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
redisCacheManager.setExpires(cacheExpiration);
return redisCacheManager;
}
Upvotes: 2