简体   繁体   中英

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 ?

My current application properties:

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

My 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? For most other Spring Boot integrations there would be some properties which would allow to directly overwrite the configuration.

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/

With this class they allow their users to set TTL in the Spring configuration, and then you can configure your Cache Managers yourself, programmatically ; see this example from the 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; either a framework such as JHipster or an end user can though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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