使用Spring Boot配置Ehcache TTL超时

时间:2019-03-04 01:45:08

标签: java spring spring-boot ehcache

如何通过常规的Spring Boot application.properties / application.yml来配置Ehcache生存时间到期?

我当前的应用程序属性:

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

我的ehcache.xml:

<config xmlns:jsr107='http://www.ehcache.org/v3/jsr107' xmlns='http://www.ehcache.org/v3'>
<service>
    <jsr107:defaults enable-management="true" enable-statistics="true"/>
</service>
<cache alias="Ttl" uses-template="ttl-template"/>
<cache-template name="ttl-template">
    <expiry>
        <ttl unit="minutes">6</ttl>
    </expiry>
    <resources>
        <heap>10000</heap>
    </resources>
 </cache-template>

主类:

@SpringBootApplication
@EnableCaching
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

是否有办法使这6分钟可配置,以便我可以在运行时/启动时覆盖设置?对于大多数其他Spring Boot集成,将有一些属性可以直接覆盖配置。

1 个答案:

答案 0 :(得分:1)

我认为您可以切换到编程配置,并像他们为Jhipster所做的那样实现一个新的Properties类:https://www.jhipster.tech/common-application-properties/

通过此类,他们允许用户在Spring配置中设置TTL,然后您可以自己以编程方式配置Cache Manager。 see this example from the ehcache3-samples repo

Spring / Spring引导正在使用他们自己的缓存抽象(Spring Cache, fully compliant with the JSR-107 spec),因此我认为提供与Ehcache3实现的进一步集成不是他们的职责。像JHipster这样的框架或最终用户都可以。

相关问题