4

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?

2
  • Spring supports many providers, including JCache (see 35.6.5) docs.spring.io/spring/docs/current/spring-framework-reference/… (Spring's implementation is very basic - a HashMap, no evict support, etc...) Commented Jan 19, 2016 at 17:52
  • 3
    In my humble opinion, JCache is highly flawed and I think it should be avoided. I was disappointed in its design decisions when implementing an adapter. Issues includes race conditions, a spec that tries to force poor algorithms, a distasteful API, design choices that are difficult to make perform well, and an ineffective TCK. I am not prepared to gather my thoughts to publicly argue against it, but I can provide some of them over private email. In your case you would want to use Spring Cache annotations, so JCache provides no extra value. Commented Jan 19, 2016 at 20:21

1 Answer 1

2

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; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.