Caching Java methods with Spring 3

A while back I wrote about caching Java methods using an earlier version of Spring, but now I am pleased to announce that you can cache your methods using annotations with Spring 3 using Ehcache annotations for Spring.

Similar to my other post you can cache a Java method in a Spring bean by simply adding the annotation.

@Cacheable(cacheName=”dogCache”)
public Collection getDogs() throws Exception {

I really put this annotation to work in my latest project because I called many Web Services in my code that return very static data that is managed by our Enterprise Service Bus. There isn’t any need to go get the data every time since it hardly ever changes.

Using the annotation, I specify that the cache region I want to use is dogCache from the ehcache.xml file and I have it configured specific to have it handle caching dogs exactly like I want it to, even to overflow to disk if necessary. Now let’s look at the configuration necessary.

You will need to drip the jar file for ehcache-annotations in your WEB-INF/lib and then make the following modifications to your applicationContext.xml.

classpath:ehcache-spring.xml

And that’s it, you are now caching your results. Be very cautious with using this, each and every method that gets cached must be thoroughly thought through. For example, I accidentally placed this annotation on a method that returned domain objects that were already configured to be cached via Hibernate, thus doubling the amount of memory that the server was using for each object loaded.

While a very powerful tool that can increase performance, caching can also decrease it.

About Chris Hardin
Chief Architect at Doozer Software in Birmingham, Al. I specialize in Java and .NET Architecture, Ajax Frameworks and Mobile Architecture with iOS and Android.

10 Responses to Caching Java methods with Spring 3

  1. Jose Noheda says:

    Very interesting. I would suggest that you simplify the configuration by adding tag or, even better, . Not having to include a config file is a plus, provide some defaults if able.I would also suggest to remove the “required” property to the cacheName attribute of the annotation if at all possible, seems like boilerplate for 90% of the cases.

  2. Casper Bang says:

    Note, I had to add org.springframework.context.support as well as CGLIB to get this to work.

  3. Ivan says:

    Nice. I developed a little to make almost the exact same thing for myself, and now I can remove it :)

  4. Eric says:

    I am one of the authors of a new project intended to provide Ehcache integration for Spring 2.5 and 3.0 projects via annotations:
    http://code.google.com/p/ehcache-spring-annotations/
    We are excited to announce the general availability of version 1.1.0
    The library provides 2 method-level annotations in the spirit of Spring’s @Transactional:
    @Cacheable
@TriggersRemove
    When appropriately configured in your Spring application, this project will create caching aspects at runtime around your @Cacheable annotated methods.
    Usage documentation can be found on the project wiki:
    http://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable
http://code.google.com/p/ehcache-spring-annotations/wiki/UsingTriggersRemove

  5. XML document does not recognize ehcache:annotation-driven. In eclipse the xml file is showing red for this.. I have added maven dependency

    com.googlecode.ehcache-spring-annotations
    ehcache-spring-annotations
    1.1.2

    and all for Spring 3.0.3

    can someone please tell why it is so? I can access the URL http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd though in IExplorer.

  6. Gong says:

    to Vinay,

    seems you are missing
    xmlns:ehcache=”http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring”
    before
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
    at the header of the xml file

  7. Jerry says:

    You need to add the “googlecode” lines in the following to your beans root XML element:

    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:task=”http://www.springframework.org/schema/task”
    xmlns:ehcache=”http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring”
    xsi:schemaLocation=”
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task.xsd
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
    “>

  8. Maan M. says:

    Caching in spring using annotations is not part of core spring and does not require this additional 3rd party JAR.

    Look at this link –> http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.html

    The annotations for evicting (removing) a cache entry is @CacheEvict.

    As mentioned in the link above, these annotations are standard way of caching java objects in spring but one can configure an external cache manager such as EhCache.

    Bottomline, you do not need this additional JAR for doing annotations based caching in spring. It is not part of core spring and it has two main annotations – @Cacheable for storing objects in the Cache and @CacheEvict. for removing or evicting the cached object.

  9. Maan M. says:

    Reposting it as I made typos in the previous post – This is now a part of core spring.

    Caching in spring using annotations is now a part of core spring and does not require this additional 3rd party JAR.
    Look at this link –> http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.html
    The annotations for evicting (removing) a cache entry is @CacheEvict.

    As mentioned in the link above, these annotations are standard way of caching java objects in spring but one can configure an external cache manager such as EhCache.

    Bottomline, you do not need this additional JAR for doing annotations based caching in spring. It is now a part of core spring and it has two main annotations – @Cacheable for storing objects in the Cache and @CacheEvict. for removing or evicting the cached object.

  10. Rajendra says:

    I have developed something similar to it long back, which caches the return object of a method based on the parameter values of the method. Now most of my team using this code, but the problem is most of the method’s parameter is Map, so everytime the toString() method of Map is different (as it is a new instace and new hash, toString will be different) though the content of the Map is same. Could you suggest me a better way to generate the same key based on the contents of the Map?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 28 other followers