配置 EhCache 3 使用默认模板自动创建所需的缓存

Geo*_*rge 5 spring-boot ehcache-3

我希望 EhCache 使用默认模板创建缓存。这是我的ehcache.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<config
    xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns='http://www.ehcache.org/v3'
    xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
  <service>
    <jsr107:defaults default-template="default" jsr-107-compliant-atomics="true">
    </jsr107:defaults>
  </service>

  <cache-template name="default">
    <expiry>
      <ttl unit="seconds">5</ttl>
    </expiry>
    <heap>100</heap>
  </cache-template>
<!--  <cache alias="test" uses-template="default" />--> <!-- I want to omit this. -->
</config>
Run Code Online (Sandbox Code Playgroud)

这是我的application.yml文件:

spring:
  cache:
    jcache:
      config: classpath:ehcache.xml
Run Code Online (Sandbox Code Playgroud)

我不想ehcache.xml每次添加需要新的缓存名称时都进行更新。如何使用 Spring Boot 配置 EhCache 以使用我的默认模板?

如果我不指定缓存名称配置,则会收到此错误:

java.lang.IllegalArgumentException: Cannot find cache named 'test' for CacheResultOperation
Run Code Online (Sandbox Code Playgroud)

jum*_*key 0

我相信您缺少此配置:

properties.put("hibernate.javax.cache.missing_cache_strategy", "create-warn"); //Use create if you don't want to see a warning
Run Code Online (Sandbox Code Playgroud)