20

Planning to implement cache mechanism for static data in spring web based application, can any one explain which is the best and how it works?

  • EhCache
  • Spring Cache
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
Delli Kilari
  • 988
  • 3
  • 14
  • 31

2 Answers2

33

Disclaimer : I am a Terracotta / Software AG employee, the maintainers of Ehcache

Ehcache is a JVM caching library, famous for being used as the default 2nd level cache for the Hibernate ORM

Spring cache was introduced in Spring 3.1, and brought annotations such as @CachePut to define uses of caches in a Spring application; by default Spring cache uses a plain Map, but you can configure it to use any popular caching framework, including Ehcache

Since Spring 4.1, Spring cache supports JSR-107, the standard for caching on the JVM.

What that means, is that you can add JSR-107 caching annotations, and then choose your caching library (ehcache 2 or 3 / guava / caffeine / etc.) : you're not tied to any caching vendor, even not tied to Spring cache annotations !

There's this nice blog post explaining the parallel between Spring cache annotations and JSR-107 annotations and if you choose to use Ehcache3 in your spring boot application, there's another interesting blog post explaining you how to integrate it in your app

Anthony Dahanne
  • 4,823
  • 2
  • 40
  • 39
  • One quirk with the JSR annotations is that there isn't protection from the thundering herd problem. That functionality requires using a loader so that the CacheResult method body isn't invoked. It gets really ugly. – Ben Manes Sep 06 '16 at 17:24
  • Thanks Lot Anthony, My doubt is that Do i need to user en-cache and spring cache ? – Delli Kilari Sep 11 '16 at 14:45
  • 2
    @DelliKilari, I suggest you enable Spring cache in your application, use Jcache / jsr107 annotations in your code and configure ehcache3 or any other jsr107 implementations as your caching implementation (see the links in my answer) – Anthony Dahanne Sep 12 '16 at 18:58
  • Thank you so much for your time to answering my doubts. – Delli Kilari Sep 17 '16 at 08:09
  • @AnthonyDahanne : Can you please give the best and quick ehcache.xml default cache configurations when implementing ehcache with hibernate 2nd level cache? – Jajikanth pydimarla Aug 09 '17 at 21:23
  • @Jajikanth : have a look at : https://github.com/ehcache/ehcache3-samples/blob/xmlconfig/fullstack/src/main/resources/ehcache.xml – Anthony Dahanne Aug 09 '17 at 21:28
  • I see it's an 3 years old answer but on ehcache site and [example code](https://github.com/gibsong/ehcache-jsr107-spring/tree/master/src/main/resources) there is still `ehcache.xml`. Is it possible to totally get rid off xml file and do only java configuration for ehcache 3.8 and spring boot 2.1 (or above)? – Iwo Kucharski May 27 '20 at 12:14
0

Ehcache3 has support for JSR 107

For Ehcache2 may be you can use this implementation

https://github.com/ehcache/ehcache-jcache/tree/master/ehcache-jcache

Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59