Caching Java methods with Spring 3
April 27, 2010 10 Comments
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.
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.
Note, I had to add org.springframework.context.support as well as CGLIB to get this to work.
Nice. I developed a little to make almost the exact same thing for myself, and now I can remove it
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
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.
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
You need to add the “googlecode” lines in the following to your beans root XML element:
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
“>
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.
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.
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?