2014-11-06 151 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元素以使其清晰。

相关问题