67,513
社区成员
发帖
与我相关
我的任务
分享
@Configuration
@EnableCaching
public class CachingConfiguration implements CachingConfigurer {
@Value("${city}")
private String city;
@Bean(destroyMethod = "shutdown")
public net.sf.ehcache.CacheManager ehCacheManager() {
net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
diskStoreConfiguration.setPath("E:\\Release\\HouseRank\\cache_" + city + "\\default");
config.diskStore(diskStoreConfiguration);
CacheConfiguration defaultCache = new CacheConfiguration();
defaultCache.setMaxEntriesLocalHeap(0);
defaultCache.setEternal(false);
defaultCache.setTimeToIdleSeconds(1200);
defaultCache.setTimeToLiveSeconds(1200);
config.addDefaultCache(defaultCache);
CacheConfiguration averagePriceCache = new CacheConfiguration();
averagePriceCache.setName("averagePriceCache");
averagePriceCache.setMaxEntriesLocalHeap(1000);
averagePriceCache.setMaxEntriesLocalDisk(10000);
averagePriceCache.setEternal(false);
averagePriceCache.setDiskSpoolBufferSizeMB(20);
averagePriceCache.setTimeToIdleSeconds(20);
averagePriceCache.setTimeToLiveSeconds(20);
averagePriceCache.setMemoryStoreEvictionPolicy("LFU");
averagePriceCache.setTransactionalMode("off");
PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();
persistenceConfiguration.setStrategy("localTempSwap");
averagePriceCache.persistence(persistenceConfiguration);
config.addCache(averagePriceCache);
CacheConfiguration hibernateCache = new CacheConfiguration();
hibernateCache.setName("org.hibernate.cache.internal.StandardQueryCache");
hibernateCache.setMaxEntriesLocalHeap(5);
hibernateCache.setEternal(false);
hibernateCache.setTimeToLiveSeconds(120);
hibernateCache.persistence(persistenceConfiguration);
config.addCache(hibernateCache);
CacheConfiguration updateTimestampCache = new CacheConfiguration();
updateTimestampCache.setName("org.hibernate.cache.spi.UpdateTimestampsCache");
updateTimestampCache.setMaxEntriesLocalHeap(5000);
updateTimestampCache.setEternal(true);
hibernateCache.persistence(persistenceConfiguration);
config.addCache(updateTimestampCache);
return net.sf.ehcache.CacheManager.newInstance(config);
}
@Bean
@Override
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheManager());
}
@Override
public CacheResolver cacheResolver() {
return null;
}
@Override
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
@Override
public CacheErrorHandler errorHandler() {
return null;
}
}
用代码注入的方式代替xml 即可用${}变量
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml,classpath*:applicationContext.xml</param-value>
</context-param>
你的config.properties 应该加载进来
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml,classpath*:applicationContext.xml:classpath:*/*.properties</param-value>
</context-param>