Spring框架5和EhCache 3.5
我尝试在基于SpringBoot2/SpringFramework5的web应用程序中使用Ehcache3.5缓存特性 我添加了EHCache依赖项:Spring框架5和EhCache 3.5,spring,spring-mvc,ehcache,ehcache-3,Spring,Spring Mvc,Ehcache,Ehcache 3,我尝试在基于SpringBoot2/SpringFramework5的web应用程序中使用Ehcache3.5缓存特性 我添加了EHCache依赖项: <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.5.0</version>
<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>
Spring启动配置:
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
但当我调用此端点时,我得到一个异常:
无法为生成器[public org.Order org.OrderController.findById(int)]找到名为“orders”的缓存缓存=[orders]| key=“”| keyGenerator=“”| cacheManager=“”| cacheResolver=“”| condition=“”|除非=“”| sync='false'
然后我尝试使用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版本2
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.3</version>
</dependency>
这里是application.yml
spring:
cache:
ehcache:
config: ehcache.xml
这里有一个带有测试计数器的服务
@Service
public class OrderService {
public static int counter=0;
@Cacheable("orders")
public Order findById(Long id) {
counter++;
return new Order(id, "desc_" + id);
}
}
下面是一个测试,以证明它正在使用缓存:
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderServiceTest {
@Autowired
private OrderService orderService;
@Test
public void getHello() throws Exception {
orderService.findById(1l);
assertEquals(1, OrderService.counter);
orderService.findById(1l);
assertEquals(1, OrderService.counter);
orderService.findById(2l);
assertEquals(2, OrderService.counter);
}
}
参见工作示例。我做了额外的研究。Spring Boot(从application.properties)未获取以下配置: 因此,我创建了jcache.xml并将其放入src/main/resource文件夹:
<config 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">
<cache alias="orders">
<key-type>org.springframework.cache.interceptor.SimpleKey</key-type>
<value-type>java.util.Collections$SingletonList</value-type>
<heap unit="entries">200</heap>
</cache>
</config>
现在,Spring缓存工作正常。然而,如何获取ehcache.xml仍然是一个问题,这里有很多东西。您正在使用的Ehcache 3通过JCache与Spring一起使用 这就是为什么需要使用
spring.cache.jcache.config=classpath:ehcache.xml
然后,您的Ehcache配置实际上是一个Ehcache 2配置。ehcachemanager
也是如此。对于JCache,应该使用JCacheCacheCacheManager
。但事实上,您甚至不需要在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>
步骤3:需要一个带有此行的application.properties
来查找ehcache.xml
spring.cache.jcache.config=classpath:ehcache.xml
注意,由于JCache位于类路径中,所以Spring缓存将选择它作为缓存提供程序。因此不需要指定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++;
}
}
面对同样的问题
- 我的ehcache.xml看起来像
20
- 在
中配置了application.properties
spring.cache.jcache.config=classpath:ehcache.xml
- 在我的应用程序类上启用
@EnableCaching
- 在我的服务实现上有
@CacheResult
- 请注意,我没有CacheManager bean
如果有人能指出我遗漏了什么,那将非常有帮助。嗯。。将ehcache.xml更改为,成功了
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
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 enable-management="true"
enable-statistics="true" />
</service>
<cache alias="vehicles" uses-template="heap-cache" />
<cache-template name="heap-cache">
<heap unit="entries">20</heap>
</cache-template>
</config>
20
谢谢。有没有办法指定ehcache.xml(不是jcache.xml)?向上投票。这在19年6月起作用。除了javax.cache
dependency之外,我已经准备好了一切。
spring.cache.jcache.config=classpath:jcache.xml
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<?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>
spring.cache.jcache.config=classpath:ehcache.xml
@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++;
}
}
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
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>
<jsr107:cache name="vehicles" template="heap-cache" />
</jsr107:defaults>
</service>
<cache-template name="heap-cache">
<heap unit="entries">20</heap>
</cache-template>
</config>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
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 enable-management="true"
enable-statistics="true" />
</service>
<cache alias="vehicles" uses-template="heap-cache" />
<cache-template name="heap-cache">
<heap unit="entries">20</heap>
</cache-template>
</config>