猿问

timetoliveseconds ehcache spring boot 配置不起作用

下面是我的 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


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


喵喵时光机
浏览 401回答 2
2回答

慕姐4208626

如果您希望缓存内容在没有交互的情况下消失,那确实不会发生。Ehcache 没有对过期项目的背景检查,可以急切地删除它们。而是内联删除过期项目,无论何时您尝试访问它们,或者如果在写入缓存期间,由于缓存已满,驱逐开始。

喵喔喔

能够使用 ehcache-JSR-107 包装器解决此问题。下面是java配置@Componentpublic class CachingSetup implements JCacheManagerCustomizer {&nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp;public void customize(CacheManager cacheManager)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;cacheManager.createCache("trans", new MutableConfiguration<>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(SECONDS, 10)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.setStoreByValue(false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.setStatisticsEnabled(true));&nbsp; &nbsp; &nbsp;}}绒球<dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-cache</artifactId> <!--Starter for using Spring Framework's caching support-->&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>javax.cache</groupId> <!-- JSR-107 API-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>cache-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.ehcache</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>ehcache</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.0.0</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; </dependencies>
随时随地看视频慕课网APP

相关分类

Java
我要回答