Spring使用Ehcache缓存机制
@Configuration
public class SpringCacheEhcacheConfig {
/**
* 缓存配置
* @return
*/
@Bean("cacheManagerFactoryBean")
public EhCacheManagerFactoryBean cacheManagerFactoryBean() {
EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
factoryBean.setConfigLocation(new ClassPathResource("spring_cache.xml"));
factoryBean.setShared(true);
return factoryBean;
}
/**
* 缓存管理器
* @return
*/
@Bean("springCacheManager")
public EhCacheCacheManager ehCacheCacheManager(@Qualifier("cacheManagerFactoryBean") CacheManager cacheManager) {
return new EhCacheCacheManager(cacheManager);
}
}
需要在src/main/resources 下建立spring_cache.xml配置文件,其配置方式在ehcache官方文档中可以了解到,我的配置比较简单,就配置了shrio使用的缓存
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="dtd/ehcache.xsd" updateCheck="false" monitoring="autodetect" dynamicConfig="true" name="spring_cache">
<diskStore path="java.io.tmpdir/spring-cache" />
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="0" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap" />
</defaultCache>
<cache name="shiro-authorizationCache" maxEntriesLocalHeap="2000" eternal="false" timeToIdleSeconds="1200" overflowToDisk="false" />
<cache name="shiro-authenticationCache" maxEntriesLocalHeap="2000" eternal="false" timeToIdleSeconds="1200" overflowToDisk="false" />
<cache name="shiro-activeSessionCache" maxEntriesLocalHeap="2000" eternal="false" timeToIdleSeconds="1200" overflowToDisk="false" />
</ehcache>
网友评论