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 : Milestone 4 reached - Even more features

Milestone 4 came with over 30 issues closed and includes new features, such as: "Official" builds of m4 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

While Ehcache 3.0 is still in development, you can give it a serious test ride today! We're working hard on bringing all the featureset from the previous releases to this new version. However the core API is very solid already and should give you a feel for what's coming...

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.0.0.m4</version>
    </dependency>      

Coding to the Ehcache 3 API


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

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

      Cache<Long, String> myCache = cacheManager.createCache("myCache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder().buildConfig(Long.class, String.class));

      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.0.0</version>
    </dependency>
You will also need an extra jar which provides to Ehcache the management capabilities required by JSR-107:
    <dependency>
      <groupId>org.terracotta</groupId>
      <artifactId>management-model</artifactId>
      <version>2.0.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.0</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!