【问题标题】:timetoliveseconds ehcache spring boot config is not workingtimetoliveseconds ehcache spring boot 配置不起作用
【发布时间】:2019-02-04 01:55:06
【问题描述】:

下面是我的 ehcache 配置文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd"
    updateCheck="true"
    monitoring="autodetect"
    dynamicConfig="true">

    <diskStore path="java.io.tmpdir" />



    <cache name="trans"
        maxEntriesLocalHeap="10000"
        maxEntriesLocalDisk="1000"
        eternal="false"
        diskSpoolBufferSizeMB="20"
        timeToIdleSeconds="0"
        timeToLiveSeconds="6"
        memoryStoreEvictionPolicy="LFU"
        transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>    
</ehcache>

所有 Spring 注释和配置都正常工作

@Component
@CacheConfig(cacheNames = {"trans" })
public class MyTransService {

    private List<Trans> list;

    @Autowired
    private EhCacheCacheManager manage;

    @PostConstruct
    public void setup() {
        list = new ArrayList<>();
    }

    @CachePut
    public void addTransaction(Trans trans) {
        this.list.add(trans);
    }

    @CacheEvict(allEntries = true)
    public void deleteAll() {
        this.list.clear();
    }    
}

但是缓存在 timetoliveseconds 之后没有被清除。

有人可以帮我看看我的配置有什么问题吗?

下面的页面说这是错误,但不知道如何解决这个问题?
我使用的是 spring-boot-starter-cache-2.0.3 版本

https://github.com/ehcache/ehcache-jcache/issues/26

有一些类似的问题,但没有提供任何解决方案

【问题讨论】:

    标签: java spring spring-boot ehcache spring-cache


    【解决方案1】:

    如果您希望缓存内容在没有交互的情况下消失,那确实不会发生。 Ehcache 没有对过期项目的后台检查,可以急切地删除它们。

    当您尝试访问它们或在写入缓存期间,会内联删除过期项目,因为缓存已满。

    【讨论】:

    • 谢谢你会检查的。能够使用 CreatedExpiryPolicy 政策解决
    【解决方案2】:

    能够使用 ehcache-JSR-107 包装器解决此问题。下面是java配置

    @Component
    public class CachingSetup implements JCacheManagerCustomizer {
    
         @Override
         public void customize(CacheManager cacheManager)
         {
           cacheManager.createCache("trans", new MutableConfiguration<>()
             .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(SECONDS, 10)))
             .setStoreByValue(false)
             .setStatisticsEnabled(true));
         }
    }
    

    波姆

    <dependencies>
            <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-cache</artifactId> <!--Starter for using Spring Framework's caching support-->
            </dependency>           
            <dependency>
              <groupId>javax.cache</groupId> <!-- JSR-107 API-->
              <artifactId>cache-api</artifactId>
            </dependency>
            <dependency>
              <groupId>org.ehcache</groupId>
              <artifactId>ehcache</artifactId>
              <version>3.0.0</version>
            </dependency>
        </dependencies>
    

    【讨论】:

      最近更新 更多