How to use Spring Cache with ehCache easily

Objective:

To demonstrate the steps to implement Spring 3.1 Cache Abstraction Implementation with ehCache. I’ll also show the spring expression language to form a key from method parameter.

Prerequisites:

1. A small standalone/web application to implement this cache implement
2. Spring 3.1
3. JDK1.5+
4. Maven2.x/3.x


Step-1

Create a ehcache.xml to set the cache configuration as below. In below code snippet I have mentioned cache name as “<methodName>” this is not mandatory but it should be unique among other caches configuration.

After creating ehcache configuration file, set into classpath based on type of application as standalone/web. e.g. -classpath, web.xml or etc.

cache configuration for ehCache implementation as ehcache.xml

Cache Config Attribute Description
name The name of the cache
maxElementsInMemory The maximum number of elements in memory, before they are evicted (0 == no limit)
eternal If eternal gets true then timeouts are ignored and the element is never expired False by default.
timeToIdleSeconds the default amount of time to live for an element from its last accessed or modified date (in seconds)
timeToLiveSeconds the default amount of time to live for an element from its creation date. It’s only used (in seconds)
overflowToDisk elements can overflow to disk when the in-memory cache has reached the set limit, flag to use disk store
memoryStoreEvictionPolicy A typesafe enumeration of eviction policies. The policy used to evict elements from the MemoryStore.
This can be one of:1. LRU – least recently used
2. LFU – least frequently used
3. FIFO – first in first out, the oldest element by creation time

To know more about the config attribute then please visit here

Step-2

Place cacheManager and ehcache configuration path in spring context configuration file e.g. ApplicationContext.xml to enable the spring annotated cache you need to add below properties

Tag Attribute Expected Value
bean  xmlns:cache https://www.springframework.org/schema/cache
xsi:schemaLocation https://www.springframework.org/schema/cache/spring-cache.xsd
 xmlns:p  https://www.springframework.org/schema/p

 

cacheManager and ehCache config in ApplicationContext.xml

 

Step-3

Now we are ready to incorporate spring 3.1 annotated cache abstraction on required method. Navigate the code on which required to add the caching as below.

cacheable annotation for spring 3.1 cache abstraction

 

In above code snippet, I used “#” to get the value from a custom object for a particular property or additional method to generate the unique key for caching using Spring Expression Language (spEL). To know more about the spEL, please click here

e.g. I have custom object with getCacheKey() method then below will be the implementation for this.

 

Sample cache key generation with custom object

To apply above key on the method should be as below:

Use of generated key and spEL

To form a key from large POJO object or JAXB object using multiple properties. Below dependency can be used to fetch the respective value without putting additonal NULL check.

To know more about JXPath click here.

JXPath Maven dependency to parse the POJO

To use above dependency to parse POJO/JAXB object below code can be used.

sample code for to use JXPath to parse JAXB object

Parsing code for JAXB using JXPath

Parsing code for POJO using JXPath

Make sure that key much be unique else it’ll override the response for different requests.

share or like this post
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Share