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

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

hcache 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:

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

Creating an In-Memory Cache

reating 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

Testing 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

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

Testing the cache

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

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

hcache 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 +786K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

Somnath Musib

Written by

Full Stack Developer, Manning Author for Spring Boot In Practice book, Technical Writer, Course Creator at Udemy. Find more at https://musibs.github.io

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store