BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Ehcache 3.0 Released with Revamped API and Off-Heap Storage

| by Matt Raible on May 02, 2016. Estimated reading time: 1 minute |

Terracotta has released version 3 of their distributed caching technology Ehcache, sporting a number of important new features. First, its API has been refactored and now leverages Java generics. Performance has generally been enhanced, and support for the javax.cache API (JSR-107) and off heap storage capabilities have been added. Also, Ehcache 3.0 is fully compatible with Spring’s caching thanks to the javax.cache API.

Developers can integrate the version of Ehcache into their Maven projects with the following coordinates:

<dependency>
  <groupId>org.ehcache</groupId>
  <artifactId>ehcache</artifactId>
  <version>3.0.0</version>
</dependency>

For Gradle users:

compile group: 'org.ehcache', name: 'ehcache', version: '3.0.0'

Ehcache’s Java API uses builders to provide a fluent means of configuration. You can interact with a Cache via a CacheManager.

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

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

myCache.put(1L, "First!");
String value = myCache.get(1L);
cacheManager.removeCache("myCache");
cacheManager.close();

Ehcache also offers a new UserManagedCache for creating caches that are not managed by a CacheManager, for example to allocate caches with a short lifecycle (see the configuration guide for more information.)

As with previous versions, Ehcache 3.0 provides storage tiers, making it possible to store frequently used data on faster-tiers and seldom-used data on slower ones.

Below is an example that shows how to configure resource pools for heap, off-heap and disk persistence. In this example, a path is provided for disk storage and three different resource pools are created. The first resource pool is sized to hold 10 entries. The second one is off-heap and will store up to 1MB. Finally, the disk resource pool is set to be 20MB. 

PersistentCacheManager persistentCacheManager = 
    CacheManagerBuilder.newCacheManagerBuilder()
        .with(CacheManagerBuilder.persistence(
            getStoragePath() + File.separator + "myData"))
        .withCache("threeTieredCache",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(
                Long.class, String.class,
                ResourcePoolsBuilder.newResourcePoolsBuilder()
                    .heap(10, EntryUnit.ENTRIES)
                    .offheap(1, MemoryUnit.MB)
                    .disk(20, MemoryUnit.MB)
                )
            ).build(true);

Developers are invited to contribute to Ehcache. (See contributor documentation for more information). For general questions and help requests, refer to the ehcache-user mailing list.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date with curated articles from top InfoQ editors

"You dont know what you dont know" change that by browsing what our editors pick for you.

BT