1. 程式人生 > >Spring Ehcache中@Cacheable不起作用。

Spring Ehcache中@Cacheable不起作用。

cts pri col time 查找文件 後綴名 數值 spring div

確認各項配置沒有問題。

一開始關於EhCache的配置放在了SpringMVC.xml中,移動到applicationContext.xml中,解決。

以下為EhCache的配置:

<!-- 啟用註解驅動的緩存 -->
<cache:annotation-driven />
<!-- 聲明緩存管理器 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:META-INF/cache.xml"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>


以下為 cache.xml :

<?xml version="1.0" encoding="UTF8"?>
<ehcache>
<!-- 當內存緩存中對象數量超過maxElementsInMemory時,將緩存對象寫到磁盤緩存中(需對象實現序列化接口) -->
<diskStore path="E:/IdeaProjects/MySSH/src/main/resources/cn/cache"/><!-- 用來配置磁盤緩存使用的物理路徑,Ehcache磁盤緩存使用的文件後綴名是*.data和*.index -->
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"/>
<!-- maxElementsOnDisk 磁盤緩存中最多可以存放的元素數量,0表示無窮大

maxElementsInMemory 內存緩存中最多可以存放的元素數量,若放入Cache中的元素超過這個數值,則有以下兩種情況
1)若overflowToDisk=true,則會將Cache中多出的元素放入磁盤文件中
2)若overflowToDisk=false,則根據memoryStoreEvictionPolicy策略替換Cache中原有的元素-->

<!--eternal 緩存中對象是否永久有效,即是否永駐內存,true時將忽略timeToIdleSeconds和timeToLiveSeconds
timeToIdleSeconds 緩存數據在失效前的允許閑置時間(單位:秒),僅當eternal=false時使用,默認值是0表示可閑置時間無窮大,此為可選屬性
timeToLiveSeconds 緩存數據在失效前的允許存活時間(單位:秒),僅當eternal=false時使用,默認值是0表示可存活時間無窮大
overflowToDisk 內存不足時,是否啟用磁盤緩存(即內存中對象數量達到maxElementsInMemory時,Ehcache會將對象寫到磁盤中)-->


<cache name="myCache"
maxElementsOnDisk="20000"
maxElementsInMemory="2000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"/>

<!--diskPersistent 是否持久化磁盤緩存,當這個屬性的值為true時,系統在初始化時會在磁盤中查找文件名為cache名稱,後綴名為index的文件
這個文件中存放了已經持久化在磁盤中的cache的index,找到後會把cache加載到內存
要想把cache真正持久化到磁盤,寫程序時註意執行net.sf.ehcache.xml.Cache.put(Element element)後要調用flush()方法
diskExpiryThreadIntervalSeconds 磁盤緩存的清理線程運行間隔,默認是120秒
diskSpoolBufferSizeMB 設置DiskStore(磁盤緩存)的緩存區大小,默認是30MB
memoryStoreEvictionPolicy========內存存儲與釋放策略,即達到maxElementsInMemory限制時,Ehcache會根據指定策略清理內存
共有三種策略,分別為LRU(最近最少使用)、LFU(最常用的)、FIFO(先進先出)-->
</ehcache>

Spring Ehcache中@Cacheable不起作用。