You need to enable JavaScript to run this app. ehcache.xml如何引用application.yml的cache-reset-time配置timeToLiveSeconds
优惠活动
大模型
产品
解决方案
定价
更多

ehcache.xml如何引用application.yml的cache-reset-time配置timeToLiveSeconds

Spring Boot 中 Ehcache.xml 引用 application.yml 配置参数实现说明

现有配置上下文

application.yml 配置片段

reset:
  servicebus:
    system-name: ${ECP_CACHE_RESET_CONSUMER_NAME:}
    notification-base-url: ${ECP_CACHE_RESET_BASEURL:}
    topic-name: ${ECP_CACHE_RESET_TOPIC:}
    cache-reset-time: 10000

ehcache.xml 现有配置片段

<cache name="refDataCache" maxElementsInMemory="15000"
       maxEntriesLocalDisk="20000" eternal="false"
       timeToIdleSeconds="300" timeToLiveSeconds="720000" overflowToDisk="true"
       memoryStoreEvictionPolicy="LFU" />

核心实现需求

将application.yml中定义的cache-reset-time参数值,用于配置ehcache.xml内refDataCache缓存的timeToLiveSeconds属性,替换原有硬编码的720000配置值。

具体实现方案

Ehcache 原生 XML 配置不直接支持读取 Spring 环境的配置值,需要依托 Spring 的属性占位符解析机制打通两类配置的关联,可根据现有项目的技术栈选择以下两种方案:

  • 方案1:保留现有 ehcache.xml 文件,接入 Spring 占位符解析能力
    1. 修改 ehcache.xml 中对应缓存的属性值,把硬编码的 TTL 替换为 Spring 占位符写法:
      <cache name="refDataCache" maxElementsInMemory="15000"
             maxEntriesLocalDisk="20000" eternal="false"
             timeToIdleSeconds="300" timeToLiveSeconds="${reset.servicebus.cache-reset-time}" overflowToDisk="true"
             memoryStoreEvictionPolicy="LFU" />
      
    2. 不要通过spring.cache.ehcache.config参数让 Spring Boot 自动加载 ehcache.xml(自动加载流程不会做占位符替换),需要通过自定义配置类加载 ehcache.xml,同时关联 Spring 环境的属性源完成占位符解析:
      @Configuration
      @EnableCaching
      public class CustomCacheConfig {
          @Bean
          public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
              EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
              // 指定项目类路径下ehcache.xml的位置
              factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
              // 关联Spring环境属性源,完成XML内占位符的解析替换
              factoryBean.setShared(true);
              return factoryBean;
          }
      }
      
  • 方案2:弃用 ehcache.xml 硬编码配置,通过 Java 配置类直接读取 yml 参数构建缓存
    该方式无需维护独立的 XML 配置文件,配置逻辑更直观,直接注入 yml 中的参数值组装缓存规则:
    @Configuration
    @EnableCaching
    public class CustomCacheConfig {
        // 直接从yml中注入cache-reset-time配置值
        @Value("${reset.servicebus.cache-reset-time}")
        private long refDataCacheTtl;
    
        @Bean
        public CacheManager cacheManager() {
            // 构建refDataCache的配置规则
            CacheConfiguration refDataCache = new CacheConfiguration("refDataCache", 15000)
                    .maxEntriesLocalDisk(20000)
                    .eternal(false)
                    .timeToIdleSeconds(300)
                    // 直接使用注入的yml参数替换原硬编码值
                    .timeToLiveSeconds(refDataCacheTtl)
                    .overflowToDisk(true)
                    .memoryStoreEvictionPolicy(EvictionPolicy.LFU);
            // 组装缓存管理器
            Configuration ehConfig = new Configuration();
            ehConfig.addCacheConfiguration(refDataCache);
            return new EhCacheManager(ehConfig);
        }
    }
    

注意事项

  • 确认配置值的单位匹配:Ehcache 的timeToLiveSeconds属性单位为秒,如果yml中定义的cache-reset-time值是毫秒级单位,需要在代码里做单位换算后再赋值,避免TTL配置偏差
  • 若保留ehcache.xml文件,需删除application.yml中自动加载ehcache的配置项(如spring.cache.ehcache.config=classpath:ehcache.xml),避免自动加载流程跳过占位符解析,导致XML内的${...}写法无法被正常识别

内容的提问来源于stack exchange,提问作者Sai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.30 17:24:01