Home > Caching, Spring n Hibernate > Caching Java methods with Spring 3

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.

Advertisement
  1. April 28, 2010 at 10:51 am | #1

    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. April 28, 2010 at 12:54 pm | #2

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

  3. April 28, 2010 at 9:44 pm | #3

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

  4. June 29, 2010 at 3:08 am | #4

    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. November 16, 2010 at 6:49 pm | #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. February 11, 2011 at 8:15 am | #6

    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. April 16, 2011 at 8:57 am | #7

    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
    “>

  1. No trackbacks yet.

Leave a Reply

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

Gravatar
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