Spring Framework 5和EhCache 3.5

我尝试在基于Spring Boot 2 / Spring Framework 5的Web应用程序中使用EhCache 3.5缓存功能.

我添加了EHCache依赖:

    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>3.5.0</version>
    </dependency>
    <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
        <version>1.0.0</version>
    </dependency>

然后在src / main / resources文件夹中创建了ehcache.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
    monitoring="autodetect" dynamicConfig="true">

    <cache name="orders" maxElementsInMemory="100" 
        eternal="false" overflowToDisk="false" 
        memoryStoreEvictionPolicy="LFU" copyOnRead="true"
        copyOnWrite="true" />
</ehcache>

Spring 5参考指南没有提到EHCache的使用,Spring 4参考指南指出:“Ehcache 3.x完全符合JSR-107标准,不需要专门的支持.”

所以我创建了控制器OrderController和REST端点:

@Cacheable("orders")
@GetMapping(path = "/{id}")
public Order findById(@PathVariable int id) {
    return orderRepository.findById(id);
}

Spring Boot配置:

@SpringBootApplication
@EnableCaching
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

但是,当我调用此端点时,我得到一个异常:

无法为Builder找到名为’orders’的缓存[public org.Order org.OrderController.findById(int)] caches = [orders] | key =”| keyGenerator =”| cacheManager =”| cacheResolver =”| condition =”|除非=”|同步=“假”

然后我尝试使用Spring Framework 4中的示例:

@Bean
public CacheManager cacheManager() {
    return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}

@Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
    EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
    cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
    cmfb.setShared(true);
    return cmfb;
}

但由于异常,它无法编译:

net.sf.ehcache.CacheManager类型无法解析.它是从所需的.class文件间接引用的

请指教.

解决方法:

这里有各种各样的东西.您正在使用的Ehcache 3通过JCache与Spring一起使用.

这就是你需要使用spring.cache.jcache.config = classpath:ehcache.xml的原因.

然后,您的Ehcache配置确实是Ehcache 2配置. EhCacheCacheManager也是如此.对于JCache,您应该使用JCacheCacheManager.但实际上,您甚至不需要使用ehcache.xml.

以下是使其工作的步骤

第1步:设置正确的依赖项.请注意,您不需要指定父pom依赖关系管理提供的任何版本. javax.cache现在是1.1版.

<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>
<dependency>
    <groupId>javax.cache</groupId>
    <artifactId>cache-api</artifactId>
</dependency>

第2步:在src / main / resources中添加ehcache.xml文件.以下是一个例子.

<?xml version="1.0" encoding="UTF-8"?>
<config
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
    xmlns='http://www.ehcache.org/v3'
    xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.5.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.5.xsd">

  <service>
    <jsr107:defaults enable-management="false" enable-statistics="true"/>
  </service>

  <cache alias="value">
    <resources>
      <heap unit="entries">2000</heap>
    </resources>
  </cache>
</config>

第3步:需要使用此行的application.properties来查找ehcache.xml

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

请注意,由于在类路径中找到了JCache,因此Spring Cache会选择它作为缓存提供程序.所以没有必要指定spring.cache.type = jcache.

第4步:像你一样启用缓存

    @SpringBootApplication
    @EnableCaching
    public class Cache5Application {

        private int value = 0;

        public static void main(String[] args) {
            ApplicationContext context = SpringApplication.run(Cache5Application.class, args);
            Cache5Application app = context.getBean(Cache5Application.class);
            System.out.println(app.value());
            System.out.println(app.value());
        }

        @Cacheable("value")
        public int value() {
            return value++;
        }
    }
上一篇:Hazelcast与Ehcache Hazelcast使用Spring和Hibernate在service / dao层缓存注释?


下一篇:idea配置echache.xml报错Cannot resolve file 'ehcache.xsd'