Thursday, January 27, 2011

EhCache integration with Spring

Previously I've used EhCache several times in applications where we would manage and retrieve cached items within our code. For example to store a cached item we may see the following:

cache.put(new Element(key, value));

And to get a cached item we may do the following:

            Element element = cache.get(key);
            if (null != element) {
                return element.getObjectValue();

On top of that we need to manage the caches using code similar to the following:
            cache = manager.getCache(name);

Several times I've asked myself 'Wouldn't it be nice if the caching mechanims were more transparent?". Caching is one of those "behind the scenes" type of operations and I never did felt comfortable putting it into our application code even if we layered it properly in multiple service tiers.

Today, I learned of EhCache and spring integration which was so easy to setup with little or no cache management. The idea is quite simple: Cache the results returned from method calls.

Here's an example of how you would declare that the results of a method be cacheable:

    @Cacheable(cacheName="mustererMouseObservations")    public Mouse getMouseObservations(String barcode) throws MustererException;

It automatically creates a cache called "mustererMouseObservations" and stores the returned result. Next time the method is invoked with the same arguments, the object returned is taken from the cache!

To declare the ehcache annotations, add the following to the spring bean wiring file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="" />
    <!-- Caching of Spring Method results -->
    <ehcache:annotation-driven create-missing-caches="true" cache-manager="ehCacheManager" />
    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <!--property name="configLocation" value="classpath:META-INF/spring/ehcache.xml" /-->

If you're using Maven like I am you can add the dependency in your pom file:
        <!--  EhCache annotations -->

And that's it! There's no need to explicity add items to the cache, or retrieve from the cache. It's all done transparently!

Be aware that cacheable methods that are self-invocating a method on itself, are not cacheable as stated as follows:


Only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual cache interception at runtime even if the invoked method is marked with @Cacheable.


No comments:

Post a Comment