【问题标题】:Spring annotation-driven ehcache note evicting using timeToLiveSeconds使用 timeToLiveSeconds 进行 Spring 注释驱动的 ehcache 注释驱逐
【发布时间】:2014-02-09 03:14:42
【问题描述】:

很抱歉,这篇文章很长,但我想在我的帖子中准确无误。

我有一个 spring mvc 3.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']

谢谢

【问题讨论】:

    标签: spring spring-mvc ehcache


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      • 2013-07-05
      • 1970-01-01
      • 2016-12-30
      • 2015-04-27
      • 1970-01-01
      相关资源
      最近更新 更多