*Ehcache3 不适用于 Spring Boot - 我尝试了下面给出的方法。 Spring boot从不缓存组件中提到的值。无论缓存是否启用,它都会被调用n次。在日志中,它显示缓存已添加到缓存管理器,但此处情况并非如此
ehcache.xml
<ehcache:config>
<ehcache:cache-template name="myDefaultTemplate">
<ehcache:expiry>
<ehcache:none/>
</ehcache:expiry>
</ehcache:cache-template>
<ehcache:cache alias="customer" uses-template="myDefaultTemplate">
<ehcache:key-type>java.lang.Long</ehcache:key-type>
<ehcache:value-type>com.controller.Customer</ehcache:value-type>
<ehcache:expiry>
<ehcache:tti unit="seconds">30</ehcache:tti>
</ehcache:expiry>
<ehcache:heap unit="entries">200</ehcache:heap>
</ehcache:cache>
</ehcache:config>
在我的 pom.xml 中,我有以下配置 -
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.6.2</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application.java 启动 Spring Boot 应用程序
@SpringBootApplication
@ComponentScan(basePackages = {"com.service"})
@EnableCaching
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
用于缓存的组件类 -
@Component
public class CustomerService {
@Cacheable(cacheNames = "customer",key="#id")
public Customer getCustomer(final Long id){
System.out.println("Returning customer information for customer id
{}
"+id);
Customer customer = new Customer();
customer.setCustomerId(id);
customer.setFirstName("Test");
customer.setEmail("contact-us@test.com");
return customer;
}
}
我尝试了几种方法,在应用程序中添加组件扫描 但没有成功。
Spring boot 启动,显示缓存已添加到缓存管理器中。
最佳答案
我通过从@Component更改为@Service来让它工作。我不明白为什么缓存在组件下不起作用而在服务层中起作用
关于java - @cacheable 不适用于 Spring Boot。使用ehcache3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56771161/