使用timeToLiveSeconds的Spring注释驱动的ehcache注释驱逐



很抱歉提前发了这么长的帖子,但我想在帖子中说得准确一点。

我有一个springmvc3.1 web应用程序,并且刚刚在我的web应用程序中构建了ehcache,我正在使用它来缓存从数据库构建的所有值列表(下拉列表)。

以下是我的设置示例。。。

<!-- Cache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.5.0</version>
</dependency>

<ehcache>
<diskStore path="java.io.tmpdir"/>      
<cache
name="lovStore"
maxElementsInMemory="512"
eternal="false"
timeToIdleSeconds="60"
timeToLiveSeconds="60"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>                       
</ehcache>

<cache:annotation-driven />
<bean id="cacheManager" 
class="org.springframework.cache.ehcache.EhCacheCacheManager" 
p:cache-manager-ref="ehcache"/>
<bean id="ehcache" 
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
p:config-location="classpath:ehcache.xml"/>

@Cacheable(value = "lovStore", key = "#code")
public Map<String, String> getLov(String code) throws ReportingManagerException {
return MockLOVHelper.getInstance().getLov(code);
}   

网上很多教程都在谈论使用@cacheEvict方法驱逐缓存,但这不适合我。我认为我更适合使用timeToLiveSeconds选项来驱逐缓存。

当我查看日志时,缓存肯定在工作,但缓存的驱逐却没有。我在网上读过一些其他文章,关于timeToLiveSeconds如何没有真正驱逐缓存,但其他文章像这样http://code.google.com/p/ehcache-spring-annotations/wiki/EvictExpiredElements它说你必须创建一些特殊的设置才能将缓存逐出。

有人能帮我了解我的缓存是否应该被驱逐,以及我如何驱逐,因为文章中提到的内容我无法理解如何实现。

以下是我的日志。但没有被驱逐的迹象。。。

2014-01-20 13:32:41,791 DEBUG [AnnotationCacheOperationSource] - Adding cacheable method 'getLov' with attribute: [CacheableOperation[public java.util.Map com.myer.reporting.dao.mock.MockLovStoreDaoImpl.getLov(java.lang.String) throws com.myer.reporting.exception.ReportingManagerException] caches=[lovStore] | condition='' | key='#code']

感谢

Ecache不会驱逐元素,直到需要这样做。这是合理的,因为没有必要浪费CPU资源进行操作(驱逐elemet),这不会有太大的区别
如果有人试图从缓存中获取元素,而该元素的生存/空闲时间将过期,则ehcache将逐出该元素并返回null。如果达到缓存/池的最大元素或内存,则ehcache将根据驱逐策略驱逐过期的元素
如果我理解正确,就不能保证所有过期元素都会被驱逐,因为只有样本元素被选择驱逐(从文档中):

@param-sampledElements这应该是总体的随机子集

最新更新