2014-11-06 58 views
4

我正在使用ehcache。我緩存春@Service方法:EhCache:爲什麼我的diskStore路徑目錄沒有創建?

@Service(value = "dataServicesManager") 
@Transactional 
public class DataServicesManager implements IDataServicesManager{ 

    @Autowired 
    private IDataDAO dataDAO; 



    @Override 
    @Cacheable(value = "alldatas") 
    public List<Data> getAllDatas(Integer param) { 

       // my logic 

     return results; 

    } 
// others services 
} 

下面是Spring配置片斷:

<cache:annotation-driven/> 

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 
    <property name="cacheManager" ref="ehcache"/> 
</bean> 
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
    <property name="configLocation" value="WEB-INF/ehcache.xml"/> 
    <property name="shared" value="true"/> 
</bean> 

這是我的Ehcache配置。

<ehcache xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true"> 

<diskStore path="C:/TEMP/ehcache"/> 

<defaultCache eternal="false" 
     timeToIdleSeconds="300" 
     timeToLiveSeconds="1200" 
     overflowToDisk="true" 
     diskPersistent="false" 
     diskExpiryThreadIntervalSeconds="120" /> 

<cache name="alldatas" maxEntriesLocalHeap="10000" eternal="false" 
     timeToIdleSeconds="21600" timeToLiveSeconds="21600" memoryStoreEvictionPolicy="LRU"> 

    </cache> 

</ehcache> 

當我打電話從Spring @Controller方法緩存和第二次呼叫服務方法getAllDatas檢索結果存儲在緩存中。 我不明白的是我找不到在ehcache.xml中指定的<diskStore path="C:/TEMP/ehcache"/>。所以我有兩個問題:

問題1:爲什麼沒有創建「C:/ TEMP/ehcache」目錄?

問題2:緩存我的服務結果在哪裏?

回答

0

可能是因爲您檢索的數據沒有溢出到磁盤。緩存在內存中完成,直到超過某個閾值。嘗試將maxEntriesLocalHeap減少到您知道的足夠小以使數據溢出並查看文件是否已創建。

+0

感謝您的回答。我改變了maxEntriesLocalHeap =「2」。而且我多次撥打我的服務,但始終沒有創建目錄。 – Pracede 2014-11-06 13:50:38

+0

從Ehcache 2.6開始,在這種情況下,所有數據都將出現在最低層的磁盤中 - 請參閱[本答案](http://stackoverflow.com/a/23358936/18591) – 2014-11-07 08:43:48

2

你的Ehcache配置是責任。

defaultCache元素將僅在以編程方式創建緩存而未指定配置時使用。

但是您明確定義了您的alldatas緩存,但沒有任何磁盤選項。

所以,你的配置需要成爲:

<cache name="alldatas" maxEntriesLocalHeap="10000" eternal="false" 
    timeToIdleSeconds="21600" timeToLiveSeconds="21600" memoryStoreEvictionPolicy="LRU" 
    overflowToDisk="true" 
    diskPersistent="false" 
    diskExpiryThreadIntervalSeconds="120"> 

</cache> 

然後該緩存將使用磁盤存儲。

如果您不打算在應用程序中使用其他緩存,則也可以刪除defaultCache元素以使其清晰。

相關問題