java - 使用 Spring Boot 配置 Ehcache TTL 超时

标签 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 集成,会有一些属性允许直接覆盖配置。

最佳答案

我认为您可以切换到编程配置并实现一个新的 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/

相关文章:

java - 从注释处理器中的注释对象获取类字段

java - 如何在 java 中使用 Windows 凭据自动登录?

java - 为什么抽象类可以这样实例化?

java - 为什么我的程序不能在 javac 编译器中运行,但可以在 IDE 中运行

java - Maven Archetype动态包名

java - 如何忽略 Java Spring Boot 项目中数据模型和 dto 的测试覆盖率

java - Spring boot中 Autowiring 接口(interface)错误

java - 从 queryForList() 方法返回数据 - 类型不兼容错误

java - 扫描 Spring Boot 应用程序中不同 Maven 模块/JAR 的组件

java - 异常: android. view.InflateException : Binary XML file line #20: Error inflating class net. simonvt.datepicker.DatePicker