[Infinispan|http://www.jboss.org/infinispan] 5.0 is just around the corner and one exciting feature awaiting its users is tight integration with Spring 3.1. What does this mean?

 

To carry a long-standing theme further, Spring 3.1 strives to turn caching into an aspect that may declaratively and largely unobtrusively applied to an application. Think declarative transactions and you get the picture.

 

More specifically, Spring 3.1 introduces two new annotations, {font:monospace}@Cacheable{font} and {font:monospace}@CacheEvict{font}, as part of a new [cache abstraction|http://blog.springsource.com/2011/02/23/spring-3-1-m1-caching/]. An example will probably prove self-explanatory:

 

{code}

public class BookDao {

 

  @Transactional

  @Cacheable(value = "books", key = "#bookId")

  Book findBook(Integer bookId) {...}

 

  @Transactional

  @CacheEvict(value = "books", key = "#bookId")

  void deleteBook(Integer bookId) {…}

}

{code}

 

Yes, this code does exactly what you think it does. Through annotating {font:monospace}findBook(Integer bookId){font} with {font:monospace}@Cacheable(value = "books", key = "#bookId"){font} Spring will cache each {font:monospace}Book{font} returned in a named cache "books", using the value of {font:monospace}bookId{font} as its cache key. Likewise, calling {font:monospace}deleteBook(Integer bookId){font} will remove the book having key {font:monospace}bookId{font} from that cache. As usual, some application context magic completes the picture:

 

{code:xml}

<beans xmlns="http://www.springframework.org/schema/beans"

            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

            xmlns:cache="http://www.springframework.org/schema/cache"

            xsi:schemaLocation="

                http://www.springframework.org/schema/beans

                http://www.springframework.org/schema/beans/spring-beans.xsd

                http://www.springframework.org/schema/cache

                http://www.springframework.org/schema/cache/spring-cache.xsd">

 

  <cache:annotation-driven />

 

</beans>

{code}

 

That's all well and good, I hear you saying, and surely testament to Spring's ongoing quest for relieving application programmers of infrastructure burdens, but where does [Infinispan|http://www.jboss.org/infinispan] come into play?

 

Well, turns out that Spring 3.1's cache abstraction has been designed with extensibility in mind. More specifically, it defines a very simple SPI third party cache vendors may implement to offer Spring users an alternative to the built-in [Ehcache|http://ehcache.org/] support. And yes, you guessed it, Infinispan 5.0 does implement this SPI. So all that's left for you, Infinispan user with a penchant for Spring (or vice versa), to do is augmenting that snippet of XML above with

 

{code:xml}

<!-- Infinispan cache manager -->

<bean id="cacheManager"

          class="org.infinispan.spring.provider.SpringEmbeddedCacheManagerFactoryBean"

          p:configurationFileLocation="classpath:/org/infinispan/spring/provider/sample/books-infinispan-config.xml" />

{code}

 

and you are good to go. Sure, there's more to be said on this topic - and yet not all that much more - but I already did that, and rather than reiterating myself I take the opportunity to refer you, kind reader, to [this|http://community.jboss.org/wiki/UsingInfinispanAsASpringCacheProvider] short introduction. Here's to hoping that you will enjoy using Infinispan's Spring support as much as we enjoyed implementing it. Happy caching!