Build High-Performance Java Applications with Ehcache

Ehcache is an open-source, standards-based cache to boosts your Java application performance, offloads database, and simplifies scalability

Somnath Musib
Nov 24, 2019 · 3 min read
Image: Unsplash by theformfitness

A cache is a temporary data store that either duplicates data stored in persistent storages or the outcome of a computation result. It is generally built with an intention to store things that will be required by an application in the future and should be accessed rapidly. Ehcache intends to allow accessing cached data repeatedly with minimal costs of time and system resources.

Ehcache is an open-source Java-based cache heavily used in enterprise applications for high throughput. It complies with Java cache API (JSR-107) with excellent integration support with the Spring and Hibernates frameworks. In this article, we will explore the basic usage of Ehcache with examples.

Getting Ehcache

Ehcache is distributed as a Maven artifact and can be downloaded from the maven repository. Presently, there are two major versions of Ehcache, Ehcache 2.X and Ehcache 3.X. Ehcache 3.X has modern type-safe APIs and configuration and is compatible with JSR-107 specification. It is recommended to use 3.X for newer applications.

Following is the maven dependency:


Creating an In-Memory Cache

Creating a cache is extremely simple with Ehcache API. Following code snippet demonstrate building a simple in-memory cache:

Ehcache implementation with an in-memory cache

Following is the summary of the cache building process:

  • We obtain a CacheManagerBuilderinstance in the very first statement
  • Subsequently, we use this builder instance to build the cache with the supplied cache name and the default configuration provided by newCacheConfigurationBuilder
  • In the cache configuration, we are specifying the key and associated value type. In addition, we are also providing the resource pool to use as heap and allowing a maximum 100 entries in that
  • We then invoke init() to initialize the cache. This instructs Ehcache to start all relevant services and initialized the cache

Following code snippet tests the usage of the cache:

Ehcache usage with an in-memory cache

In this unit test case, we are building a very simple cache named myCache with key type integer and value type String. We are then putting the value Medium with the key as 1. Finally, we are asserting the cache contents with the value.

Creating a persistent storage cache

At times, it might not be possible to up & running the entire cache in-memory and you would like to use your disk as the cache storage. Following example demonstrates how to do this:

Ehcache implementation with persistent storage

Above configuration is similar to the in-memory configuration except for two configurations:

  • We are now initializing the cache with a directory path so that cached data can be stored in this directory
  • There is a 10 MB space allocated in the disk

Below unit test case demonstrates the usage of disk storage based cache. In this test, we are storing 5000 records (i.e. key-value pairs). In our persistent cache heap storage is only limited to 100 entries, hence Ehcache uses the file storage to store and retrieve the data

Ehcache usage with persistent storage

Expiring Cached Data

In the previous two sections, we have seen how to create caches and use them. In those examples, data is kept in the cache till the time application JVM is up & running. However, at times we would like to expire the cached data after a certain interval of time. Ehcache allows us to do that while configuring the cache by specifying the expiry configuration. This is demonstrated below:

Ehcache implementation with data expiry

Note the withExpiry clause is added in the previously used default configuration to expire data after the configured duration. Following test case demonstrates this concept:

Ehcache usage with data expiry

Wrapping Up

Ehcache is an excellent, high-performance caching provider to cached frequently referred application data. It is highly configurable with room for a developer to override the default configurations with custom implementations.

The Startup

Get smarter at building your thing. Join The Startup’s +729K followers.