|
||||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |
See:
Description
Class Summary | |
---|---|
ContainerCacheManagerFactoryBean | FactoryBean for creating a CacheManager for a pre-defined CacheContainer . |
SpringCache<K,V> |
A implementation that delegates to a
instance supplied at construction
time. |
SpringEmbeddedCacheManager |
A implementation that is
backed by an instance. |
SpringEmbeddedCacheManagerFactoryBean |
A for creating an
instance. |
SpringRemoteCacheManager |
A implementation that is
backed by an instance. |
SpringRemoteCacheManagerFactoryBean |
A for creating an
instance. |
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:
Cache
, Spring's cache abstraction itself, and
CacheManager
, a service for creating Cache
instances
While Spring Infinispan offers only one implementation of org.springframework.cache.Cache
, namely
, there are two implementations
of org.infinispan.spring.provider.SpringCache
org.springframework.cache.CacheManager
:
org.infinispan.spring.provider.SpringEmbeddedCacheManager
and
org.infinispan.spring.provider.SpringRemoteCacheManager
.
org.infinispan.manager.EmbeddedCacheManager
and its default
implementation org.infinispan.manager.DefaultCacheManager
. The
latter backs SpringEmbeddedCacheManager
.
org.infinispan.client.hotrod.RemoteCacheManager
. SpringRemoteCacheManager
delegates to it.
Using Spring Infinispan as a Spring Cache provider may be divided into two broad areas:
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
.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
A detailed discussion about how to use Spring's caching annotations
and @Cacheable
is beyond this documentation's scope. A simple example may
serve as a starting point:
@CacheEvict
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.
|
--> |