简体   繁体   English

使用Spring Boot使Ehcache TTL超时可配置

[英]Make Ehcache TTL timeout configurable with Spring Boot

How can I make the Ehcache Time To Live expiration configurable through the regular Spring Boot application.properties / application.yml ? 如何通过常规的Spring Boot application.properties / application.yml配置Ehcache生存时间到期?

My current application properties: 我当前的应用程序属性:

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

My 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>

Main class: 主班:

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

Is there a way to make those 6 minutes configurable so that I can overwrite the setting at runtime / on startup? 有没有办法使这6分钟可配置,以便我可以在运行时/启动时覆盖设置? For most other Spring Boot integrations there would be some properties which would allow to directly overwrite the configuration. 对于大多数其他Spring Boot集成,将有一些属性可以直接覆盖配置。

I think you could switch to programmatic configuration and implement a new Properties class like the one they did for Jhipster : https://www.jhipster.tech/common-application-properties/ 我认为您可以切换到编程配置,并像他们为Jhipster所做的那样实现一个新的Properties类: https ://www.jhipster.tech/common-application-properties/

With this class they allow their users to set TTL in the Spring configuration, and then you can configure your Cache Managers yourself, programmatically ; 通过此类,他们允许用户在Spring配置中设置TTL,然后您可以自己以编程方式配置Cache Manager。 see this example from the ehcache3-samples repo . 请参阅ehcache3-samples repo中的示例

Spring / Spring boot are using their own cache abstractions ( Spring Cache, fully compliant with the JSR-107 spec ) , so I don't think it's their role to provide further integration with the Ehcache3 implementation; Spring / Spring启动正在使用他们自己的缓存抽象( Spring Cache,完全符合JSR-107规范 ),因此我认为提供与Ehcache3实现的进一步集成不是他们的职责; either a framework such as JHipster or an end user can though. 诸如JHipster之类的框架或最终用户都可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM