Spring Caching with Pivotal GemFire
Get Gemfire
First you have to get a trial copy from Pivotal Gemfire.
Install it into your system, and make sure it runing.
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 GuavaCacheManager
provided by Spring.@Bean CacheFactoryBean cacheFactoryBean() { return new CacheFactoryBean(); } @Bean LocalRegionFactoryBean<String, Conference> localRegionFactory(final GemFireCache cache) { return new LocalRegionFactoryBean<String, Conference>() {{ setCache(cache); setName("conference"); }}; } @Override @Bean public CacheManager cacheManager() { GemfireCacheManager _cacheManager= new GemfireCacheManager(); try { _cacheManager.setCache(cacheFactoryBean().getObject()); Set<Region<?,?>> regions=new HashSet<>(); regions.add(localRegionFactory(cacheFactoryBean().getObject()).getObject()); _cacheManager.setRegions(regions); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return _cacheManager; } @Override @Bean public KeyGenerator keyGenerator() { return new SimpleKeyGenerator(); }
Do not forget to add the spring-data-gemfire dependency in your Maven pom.xml file.
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-gemfire</artifactId> </dependency>
Code sample
Check out the sample codes.
And explore the spring-cache-gemfire folder.
评论