有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ehcache3为什么缓存没有过期?

我想缓存一些dao查询。它可以工作,但缓存不会过期。我做错了什么

我的ehcache。xml:

<config xmlns='http://www.ehcache.org/v3'
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
                            http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">

    <cache alias="test">
    <key-type>java.lang.String</key-type>
    <value-type>xxx.model.SignalValueType</value-type>
        <expiry>
            <ttl unit="seconds">5</ttl>
        </expiry>

        <resources>
            <heap unit="entries">100</heap>
            <offheap unit="MB">10</offheap>
        </resources>
    </cache> 
</config>

正在申请中。我有一个条目:

spring.cache.jcache.config=classpath:ehcache.xml

在pom中:

<dependency>
    <groupId>javax.cache</groupId>
    <artifactId>cache-api</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>

我的问题是:

@Cacheable(value = "test", key = "#code")
@Override
public SignalValueType getSignalValueType(String code) {         
  SignalValueType signalType = (SignalValueType) sf.getCurrentSession()
.createQuery("FROM SignalValueType svt WHERE svt.code = :code").setParameter("code", code)
.uniqueResult();

 return signalType;
}

我预计会有5秒过期,但在这段时间(以及更长时间)之后,查询仍然被缓存。我做错了什么


共 (1) 个答案

  1. # 1 楼答案

    不应该是ehcache而不是jcache

    spring.cache.ehcache.config= {ehcache config location}