首先是常見的注解配置
@Configuration
@EnableCaching
public class WebConfiguration extends WebMvcConfigurerAdapter{
/**
* ehcache主要的管理器
* @return
*/
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
System.out.println("CacheConfiguration.ehCacheManagerFactoryBean()");
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("conf/ehcache.xml"));
cacheManagerFactoryBean.setShared(true);
return cacheManagerFactoryBean;
}
@Bean
@Autowired
public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean ehCacheManagerFactoryBean){
System.out.println("CacheConfiguration.ehCacheCacheManager()");
return new EhCacheCacheManager(ehCacheManagerFactoryBean.getObject());
}
@Bean
public KeyGenerator customerKeyGenerator(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(":")
.append(method.getName());
for(Object obj:params){
sb.append(":")
.append(obj.toString());
}
return sb.toString();
}
};
}
}
其次是ehcache的配置文件描述
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<!--
diskStore:為緩存路徑,ehcache分為內(nèi)存和磁盤兩級秫筏,此屬性定義磁盤的緩存位置梯投。參數(shù)解釋如下:
user.home – 用戶主目錄
user.dir – 用戶當(dāng)前工作目錄
java.io.tmpdir – 默認(rèn)臨時文件路徑
-->
<diskStore path="java.io.tmpdir/Tmp_EhCache" />
<!--
defaultCache:默認(rèn)緩存策略躯砰,當(dāng)ehcache找不到定義的緩存時蛾默,則使用這個緩存策略静尼。只能定義一個塔逃。
-->
<!--
name:緩存名稱挖炬。
maxElementsInMemory:緩存最大數(shù)目
maxElementsOnDisk:硬盤最大緩存?zhèn)€數(shù)揽浙。
eternal:對象是否永久有效,一但設(shè)置了意敛,timeout將不起作用馅巷。
overflowToDisk:是否保存到磁盤,當(dāng)系統(tǒng)當(dāng)機(jī)時
timeToIdleSeconds:設(shè)置對象在失效前的允許閑置時間(單位:秒)草姻。僅當(dāng)eternal=false對象不是永久有效時使用钓猬,可選屬性,默認(rèn)值是0撩独,也就是可閑置時間無窮大敞曹。
timeToLiveSeconds:設(shè)置對象在失效前允許存活時間(單位:秒)账月。最大時間介于創(chuàng)建時間和失效時間之間。僅當(dāng)eternal=false對象不是永久有效時使用异雁,默認(rèn)是0.捶障,也就是對象存活時間無窮大。
diskPersistent:是否緩存虛擬機(jī)重啟期數(shù)據(jù) Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskSpoolBufferSizeMB:這個參數(shù)設(shè)置DiskStore(磁盤緩存)的緩存區(qū)大小纲刀。默認(rèn)是30MB项炼。每個Cache都應(yīng)該有自己的一個緩沖區(qū)。
diskExpiryThreadIntervalSeconds:磁盤失效線程運(yùn)行時間間隔示绊,默認(rèn)是120秒锭部。
memoryStoreEvictionPolicy:當(dāng)達(dá)到maxElementsInMemory限制時,Ehcache將會根據(jù)指定的策略去清理內(nèi)存面褐。默認(rèn)策略是LRU(最近最少使用)拌禾。你可以設(shè)置為FIFO(先進(jìn)先出)或是LFU(較少使用)。
clearOnFlush:內(nèi)存數(shù)量最大時是否清除展哭。
memoryStoreEvictionPolicy:可選策略有:LRU(最近最少使用湃窍,默認(rèn)策略)、FIFO(先進(jìn)先出)匪傍、LFU(最少訪問次數(shù))您市。
FIFO,first in first out役衡,這個是大家最熟的茵休,先進(jìn)先出。
LFU手蝎, Less Frequently Used榕莺,就是上面例子中使用的策略,直白一點(diǎn)就是講一直以來最少被使用的棵介。如上面所講钉鸯,緩存的元素有一個hit屬性,hit值最小的將會被清出緩存邮辽。
LRU亏拉,Least Recently Used,最近最少使用的逆巍,緩存的元素有一個時間戳,當(dāng)緩存容量滿了莽使,而又需要騰出地方來緩存新的元素的時候锐极,那么現(xiàn)有緩存元素中時間戳離當(dāng)前時間最遠(yuǎn)的元素將被清出緩存。
-->
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="180"
memoryStoreEvictionPolicy="LRU" />
<cache
name="demo"
eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="180"
memoryStoreEvictionPolicy="LRU" />
<!-- 驗(yàn)證碼緩存 1 分鐘 -->
<cache name="captchaCache" maxEntriesLocalHeap="2000" eternal="false"
timeToIdleSeconds="60" timeToLiveSeconds="60" overflowToDisk="false"
statistics="true">
</cache>
<cache name="permissionCache" maxEntriesLocalHeap="2000" eternal="false"
timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"
statistics="true">
</cache>
</ehcache>