Spring Caching with Infinispan
JBoss Infinispan is popular distributed caching solution, it is the base of RedHad Data Grid product.
In the real world projects, Infinispan can work in several ways.
- Run as a standalone server.
- Run as JBoss service.
- Embedded.
Infinispan also provides Spring integration, and provides two different
CacheManager
implementations for embedded way and standalone servers.
This post focus on the embedded way, the next post will contain the standalone environment configuration.
Enable Caching
Add
@EnableCaching(mode=AdviceMode.ASPECTJ)
annotation on the configuration class if you are using Java based configuration.@Configuration ... @EnableCaching(mode=AdviceMode.ASPECTJ) public class JpaConfig {...}
Then, specify a
CacheManager
provider in your configuration. There is a SpringEmbeddedCacheManager
provided by Infinispan Spring integration.@Override @Bean public CacheManager cacheManager() { return new SpringEmbeddedCacheManager( new DefaultCacheManager( new ConfigurationBuilder() .eviction() .maxEntries(20000) .strategy(EvictionStrategy.LIRS) .expiration() .wakeUpInterval(5000L) .maxIdle(120000L) .build() ) ); } @Override @Bean public KeyGenerator keyGenerator() { return new SimpleKeyGenerator(); }
Do not forget to add the infinispan-spring dependency in your Maven pom.xml file.
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-spring</artifactId> </dependency>
Code sample
Check out the sample codes.
And explore the spring-cache-infinispan-embedded folder.
评论