Using Spring 3.1 Cache Abstraction and Ehcache

Spring 3.1 supports a new cache abstraction layer which allows you to implement caching in a web application beyond the L2 and query cache in Hibernate. This blog provides a quick walkthrough on how to setup and apply caching  in your codes using Spring and Ehcache.

Getting the jars

First, we need to add the spring context support and ehcache jar files to the project. I am using Maven and include the following dependencies in the pom file:



Configure Spring

As with any other Spring components, we need to configure some beans in a XML file. At a minimum, we will need the followings:

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”;

<cache:annotation-driven cache-manager=”cacheManager”/>

<bean id=”cacheManager”>
<property name=”cacheManager” ref=”ehcache”/>

<!– Ehcache library setup –>
<bean id=”ehcache”
<property name=”configLocation” value=”classpath:ehcache.xml”/>


Caching is setup via annotation, as configured in the tag cache:annotation-driven. Next a cache manager is defined and is referenced to a factory bean for instantiating Ehcache. The bean org.springframework.cache.ehcache.EhCacheManagerFactoryBean configLocation property is set to the ehcache.xml setup files where we will define the caches used by the application.

The ehcache.xml config file

I recommend starting with the ehcache-failsafe.xml found in the ehcache-core jar file and add the caches required by the application. An example for this blog is included below:

<!– Sample cache named sampleCache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes.

If there are more than 10000 elements it will overflow to the
disk cache,
<cache name=”sampleCache1″

It is also necessary to set the directory to store the files when caches overflow into disk storage. To do this, update the path attribute in the diskStore tag:

 <diskStore path=”${CACHE_DIR}”/>

Note the use of variable CACHE_DIR above. This will allow us to define the directory using the Java JVM variable (-DCACHE_DIR) on different environments.

Adding Cache Annotations

Now we are ready to do something useful. To add caching to a method, use the @Cacheable annotation as shown below. I typically would add caching in the service layer.

    @Cacheable(value=”sampleCache1″, key=”#id”)
public Product load(String id) {“load product with id” + id);
return repository.load(id);

I recommend setting the key explicitly instead of relying on the default key generator. Alternatively, you could configure your own key generator.

To evict an object in the cache, use @CacheEvict annotation. For example, we would have to evict the object in the cache in the update method.

    @CacheEvict(value=”sampleCache1″, key=””, beforeInvocation=false /*default*/, allEntries=false /*default*/)
public void update(Product product) {

@CacheEvict annotation provides 2 handy attributes: beforeInvocation can be set to evict the object in the cache before or after (default) the method is executed. allEntries can be used to evict all objects in the cache. Only the object with the key is evicted by default.