Ehcache is an open source, standards-based cache that boosts performance, offloads your database, and simplifies scalability. It's the most widely-used Java-based cache because it's robust, proven, full-featured, and integrates with other popular libraries and frameworks. Ehcache scales from in-process caching, all the way to mixed in-process/out-of-process deployments with terabyte-sized caches.


Ehcache 3.10 is now available!

The major feature in this release is addition of new variant JARs that adopt the Jakarta EE namespaced JTA and JAXB APIs.

It requires Java 8+.

Watch for continued rapid improvements to features, stability and performance of the 3.x line throughout the coming months!

As a reminder, Ehcache 3 introduced the following: "Official" builds are available on the project's GitHub release page or from Maven Central - see below.

Getting started with Ehcache 3

You can introduce caching to your Java application quite easily with Ehcache, either using its new, powerful API or using the javax.cache API as defined in the JSR-107 specification.

Using the core Ehcache v3 API

Ehcache 3 has a streamlined, modernized type-safe API (and configuration) that will greatly improve your coding experience when compared to Ehcache 2.x.

Downloading the jars

You can download the jar directly from github or get it from Maven Central:
    <dependency>
      <groupId>org.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>3.10.0</version>
    </dependency>      

Coding to the Ehcache 3 API


      CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
          .withCache("preConfigured",
               CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
                                              ResourcePoolsBuilder.heap(100))
               .build())
          .build(true);

      Cache<Long, String> preConfigured
          = cacheManager.getCache("preConfigured", Long.class, String.class);

      Cache<Long, String> myCache = cacheManager.createCache("myCache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
                                        ResourcePoolsBuilder.heap(100)).build());

      myCache.put(1L, "da one!");
      String value = myCache.get(1L);

      cacheManager.close();

      

Using the JSR-107 API

Downloading the jars

You'll need the Ehcache distribution as mentioned above, you'll also require the actual JSR-107 API:
    <dependency>
      <groupId>javax.cache</groupId>
      <artifactId>cache-api</artifactId>
      <version>1.1.0</version>
    </dependency>

Not quite ready yet?

Read the user documentation for everything you've been wondering about the new API!

Getting started with Ehcache 2

You can introduce caching to your Java application quiet easily with Ehcache 2.

Using the core Ehcache v2 API

Downloading the jars

You can download the jar directly from ehcache.org or get it from Maven Central:
    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>2.10.3</version>
    </dependency>      

Coding to the Ehcache 2 API


      // Create a cache manager
      final CacheManager cacheManager = new CacheManager();

      // create the cache called "hello-world"
      final Cache cache = cacheManager.getCache("hello-world");

      // create a key to map the data to
      final String key = "greeting";

      // Create a data element
      final Element putGreeting = new Element(key, "Hello, World!");

      // Put the element into the data store
      cache.put(putGreeting);

      // Retrieve the data element
      final Element getGreeting = cache.get(key);

      // Print the value
      System.out.println(getGreeting.getObjectValue());
      

Not quite ready yet?

Read the user documentation for everything you've ever wondered about the Ehcache 2 API!