如何通过常规 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 集成,会有一些属性允许直接覆盖配置。
最佳答案
我认为您可以切换到编程配置并实现一个新的 Properties 类,就像他们为 Jhipster 所做的那样:https://www.jhipster.tech/common-application-properties/
通过这个类,他们允许用户在 Spring 配置中设置 TTL,然后您可以自己以编程方式配置您的缓存管理器; see this example from the ehcache3-samples repo .
Spring/Spring boot 使用它们自己的缓存抽象 ( Spring Cache, fully compliant with the JSR-107 spec ),所以我认为它们的作用不是提供与 Ehcache3 实现的进一步集成; JHipster 等框架或最终用户都可以。
关于java - 使用 Spring Boot 配置 Ehcache TTL 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54975613/