Call us: +1-415-738-4000
Ehcache has had excellent Spring integration for many years. More recently there are two new ways of using Ehcache with Spring
Spring Framework 3.1 added a new generic cache abstraction for transparently applying caching to Spring applications. It adds caching support for classes and methods using two annotations:
Cache a method call. In the following example, the value is the return type, a Manual. The key is extracted from the ISBN argument using the id.
@Cacheable(value="manual", key="#isbn.id") public Manual findManual(ISBN isbn, boolean checkWarehouse)
Clears the cache when called.
@CacheEvict(value = "manuals", allEntries=true) public void loadManuals(InputStream batch)
Spring 3.1 includes an Ehcache implementation. See the Spring 3.1 JavaDoc.
It also does much more with SpEL expressions. See http://blog.springsource.com/2011/02/23/spring-3-1-m1-caching/ for an excellent blog post covering this material in more detail.
This open source, led by Eric Dalquist, predates the Spring 3.1 project. You can use it with earlier versions of Spring or you can use it with 3.1.
As with Spring 3.1 it uses an @Cacheable annotation to cache a method. In this example calls to findMessage are stored in a cache
named "messageCache". The values are of type
Message. The id for each entry is the
id argument given.
@Cacheable(cacheName = "messageCache") public Message findMessage(long id)
And for cache invalidation, there is the @TriggersRemove annotation.
In this example,
cache.removeAll() is called after the method is invoked.
@TriggersRemove(cacheName = "messagesCache", when = When.AFTER_METHOD_INVOCATION, removeAll = true) public void addMessage(Message message)
See http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/ for a blog post explaining its use and providing further links.