工作中遇到的问题--缓存配置(使用@Configuration装配 @Bean的方式注入)

时间:2021-12-08 22:54:52

@EnableCaching
@Configuration
public class MFGCachingConfiguration {

@Autowired
private MFGSettings mfgSettings;

@Bean(name="MFGKeyGenerator")
public KeyGenerator MFGKeyGenerator(){
SimpleKeyGenerator defaultKeyGen = new SimpleKeyGenerator();
KeyGenerator mfgKeyGen = new KeyGenerator(){

@Override
public Object generate(Object target, Method method, Object... params) {
if(method != null){
return method.getName() + defaultKeyGen.generate(target, method, params);
} else {
return defaultKeyGen.generate(target, method, params);
}
}

};
return mfgKeyGen;
}

@Bean
public CacheManager getEhCacheManager() {
CacheConfiguration ehCacheConf = new CacheConfiguration();
ehCacheConf.setName("dashboard_cache");
ehCacheConf.setMaxEntriesLocalDisk(1000l);
ehCacheConf.setMaxEntriesLocalHeap(5000l);
ehCacheConf.setEternal(false);
ehCacheConf.setDiskSpoolBufferSizeMB(50);
int dashboardCacheCleanUpSchedulerFrequency = Integer.parseInt(mfgSettings.getDashboardCacheCleanUpSchedulerFrequency());
ehCacheConf.setTimeToIdleSeconds(dashboardCacheCleanUpSchedulerFrequency);
ehCacheConf.setTimeToLiveSeconds(dashboardCacheCleanUpSchedulerFrequency);
ehCacheConf.setMemoryStoreEvictionPolicy("LFU");
ehCacheConf.setTransactionalMode("OFF");
ehCacheConf.addPersistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP));

net.sf.ehcache.config.Configuration configration = new net.sf.ehcache.config.Configuration();
configration.setName("dashboard_cache");
configration.addCache(ehCacheConf);

CacheManager cacheManager = new EhCacheCacheManager(new net.sf.ehcache.CacheManager(configration));
return cacheManager;
}
}