Package org.infinispan.spring.provider

Spring Infinispan - An implementation of Spring 3.1's Cache SPI based on JBoss Infinispan.

See: Description

Package org.infinispan.spring.provider Description

Spring Infinispan - An implementation of Spring 3.1's Cache SPI based on JBoss Infinispan.

Spring 3.1 introduces caching capabilities a user may comfortably utilize via a set of custom annotations, thus telling the Spring runtime which objects to cache under which circumstances.
Out of the box, Spring ships with EHCache as the caching provider to delegate to. It defines, however, a simple SPI vendors may implement for their own caching solution, thus enabling Spring users to swap out the default EHCache for another cache of their choosing. This SPI comprises two interfaces:

Spring Infinispan implements this SPI for JBoss Infinispan.

While Spring Infinispan offers only one implementation of org.springframework.cache.Cache, namely org.infinispan.spring.provider.SpringCache, there are two implementations of org.springframework.cache.CacheManager:

  1. org.infinispan.spring.provider.SpringEmbeddedCacheManager and
  2. org.infinispan.spring.provider.SpringRemoteCacheManager.
These two implementations cover two distinct use cases:
  1. Embedded: Embed your Spring-powered application into the same JVM running an Infinispan node, i.e. every communication between application code and Infinispan is in-process. Infinispan supports this use case via the interface org.infinispan.manager.EmbeddedCacheManager and its default implementation org.infinispan.manager.DefaultCacheManager. The latter backs SpringEmbeddedCacheManager.
  2. Remote: Application code accesses Infinispan nodes remotely using Infinispan's own hotrod protocol. Infinispan supports this use case via org.infinispan.client.hotrod.RemoteCacheManager. SpringRemoteCacheManager delegates to it.

Usage

Using Spring Infinispan as a Spring Cache provider may be divided into two broad areas:

  1. Telling the Spring runtime to use Spring Infinispan and therefore Infinispan as its caching provider.
  2. Using Spring's caching annotations in you application code.

Register Spring Infinispan with the Spring runtime

Suppose we want to use Spring Infinispan running in embedded mode as our caching provider, and suppose further that we want to create two named cache instances, "cars" and "planes". To that end, we put

 <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"
        xmlns:p="http://www.springframework.org/schema/p"
        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 />
     
     <bean id="cacheManager" class="org.infinispan.spring.SpringEmbeddedCacheManagerFactoryBean"
              p:configuration-file-location="classpath:/org/infinispan/spring/embedded/example/infinispan-sample-config.xml"/>

 </beans>
 
in our Spring application context. It is important to note that classpath:/org/infinispan/spring/embedded/example/infinispan-sample-config.xml points to a configuration file in the standard Infinispan configuration format that includes sections for two named caches "cars" and "planes". If those sections are missing the above application context will still work, yet the two caches "cars" and "planes" will be configured using the default settings definded in classpath:/org/infinispan/spring/embedded/example/infinispan-sample-config.xml.
To further simplify our setup we may omit the reference to an Infinispan configuration file in which case the underlying org.infinispan.manager.EmbeddedCacheManager will use Infinispan's default settings.

For more advanced ways to configure the underlying Infinispan EmbeddedCacheManager see org.infinispan.spring.provider.SpringEmbeddedCacheManagerFactoryBean.

If running Infinispan in remote mode the above configuration changes to

 <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"
        xmlns:p="http://www.springframework.org/schema/p"
        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 />
     
     <bean id="cacheManager" class="org.infinispan.spring.SpringEmbeddedCacheManagerFactoryBean"
              p:configuration-properties-file-location="classpath:/org/infinispan/spring/remote/example/hotrod-client-sample.properties"/>

 </beans>
 

For more advanced ways to configure the underlying Infinispan RemoteCacheManager see org.infinispan.spring.provider.SpringRemoteCacheManagerFactoryBean.

Using Spring's caching annotations in application code

A detailed discussion about how to use Spring's caching annotations @Cacheable and @CacheEvict is beyond this documentation's scope. A simple example may serve as a starting point:

 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Repository;

 @Repository
 public class CarRepository {

   @Cacheable("cars")
   public Car getCar(Long carId){
       ...
   }
   
   @CacheEvict(value="cars", key="car.id")
   public void saveCar(Car car){
       ...
   }
 }
 
In both @Cache("cars") and @CacheEvict(value="cars", key="car.id") "cars" refers to the name of the cache to either store the returned Car instance in or to evict the saved/updated Car instance from. For a more detailed explanation of how to use @Cacheable and @CacheEvict see the relevant reference documentation chapter.

-->

Copyright © 2017 JBoss, a division of Red Hat. All Rights Reserved.