Category Archives: ehcache

Simple ehCache Example

Problem Statement

I have a data provider (basically, system-of-record) which can return me a huge data set. What I want to do is, write these records in a cache, and then, return a set of records page-by-page from the cache. The cache here acts like a buffer to ensure that there is no strain on the memory. In the following example, I have just stopped short of the actual paging solution. Instead I have tried to illustrate how its possible using EhCache.

Maven Configuration

Just putting ehcache and slf4j should be good enough.

EhCache Configuration

The ehCache configuration file, my-demo-ehcache.xml in this case, looks like:

Getting a handle to the Cache

Note that we are loading the ehCache configuration file and then retrieving a cache named myCache1, which I had defined in the configuartion xml.

 Putting stuff into the cache

If you are planning to write this cache on a disk, then you need to make sure that the object that you are storing is Serializable.

Retrieving stuff from the cache

Resources

The sources for this can be found here: https://github.com/paawak/blog/tree/master/code/ehcache-simple