下面列出了怎么用org.springframework.cache.jcache.JCacheCacheManager的API类实例代码及写法,或者点击链接到github查看源代码。
@Bean
public CacheManager cacheManager(@Autowired Config config) {
long ttl = Long.valueOf(config.getProperties().getProperty(Config.PROP_CACHE_TTL));
config.getProperties().getProperty(Config.PROP_CACHE_SIZE);
MemSize memSize = MemSize.valueOf(config.getProperties().getProperty(Config.PROP_CACHE_SIZE));
LogFactory.getLog(getClass()).info(
String.format("Initializing cache TTL=%d secs, size=%d %s",
ttl, memSize.getSize(), memSize.getUnit().toString()));
org.ehcache.config.CacheConfiguration<Object, Object> cacheConfiguration = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Object.class, Object.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(memSize.getSize(), memSize.getUnit()))
.withExpiry(Expirations.timeToLiveExpiration(new org.ehcache.expiry.Duration(ttl, TimeUnit.SECONDS)))
.build();
Map<String, CacheConfiguration<?, ?>> caches = new HashMap<>();
caches.put(CACHE_DEFAULT, cacheConfiguration);
EhcacheCachingProvider provider = (EhcacheCachingProvider) javax.cache.Caching.getCachingProvider();
// when our cacheManager bean is re-created several times for
// diff test configurations, this provider seems to hang on to state
// causing cache settings to not be right. so we always close().
provider.close();
DefaultConfiguration configuration = new DefaultConfiguration(
caches, provider.getDefaultClassLoader());
return new JCacheCacheManager(
provider.getCacheManager(provider.getDefaultURI(), configuration));
}
@Bean
@Override
public org.springframework.cache.CacheManager cacheManager() {
return new JCacheCacheManager(environment.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_PRODUCTION) ?
createClusteredCacheManager() : createInMemoryCacheManager());
}