java - 为什么 Spring Boot 缓存配置不起作用
问题描述
我有一个简单的 Spring Boot 应用程序,它与自定义 jar 交互以从第三方应用程序获取数据。
我的 Spring Boot 应用程序类如下所示:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
我有一个这样的控制器(它调用一个服务方法):
@RequestMapping(value = {"/getTestData.spr"}, method = RequestMethod.GET)
public JsonView getTestData(@RequestParam(value = "childProjectIdKeyName", required = false) String childProjectIdKeyName) {
//Get All Test Case Data
testRunsFromTracker = testRunProgressService.getAllTestRunListByProjects(childProjectIdKeyName);
//Some other logic that returns a JSON View to the front-end
return new JsonView(someManipulatedCollectionDataHere)
}
被调用的服务类如下所示:
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class TestService {
@Cacheable (cacheNames = "testRun", key = "#childProjectIdKeyName")
public List<TrackerItemDto> getAllTestRunListByProjects(String childProjectIdKeyName) {
System.out.println(String.format("-----getAllTestRunListByProjects(%s)-----", childProjectIdKeyName));
//-----some complex code that take a long time to execute everytime it is called-----//
}
我有我pom.xml
的包含以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
每次我调用在提到的调用的控制器时TestService
,我都会打印控制台,说明我仍然缺少一些使缓存工作的东西。每次都调用服务方法,这在缓存实现之后是不期望的。
我在这里错过了什么吗?
解决方案
推荐阅读
- api - 取决于经过身份验证的用户的 API 端点
- graphql - 将 ImageSharp 作为字段添加到 MarkdownRemark 节点(不是 frontmatter)
- r - For循环填充R中的一列
- python-3.x - 这在 Python 中是如何发生的?
- python - 函数的参数可以用作全局变量的占位符吗
- http - 根据查询的路由使用不同的请求解析器
- javascript - 动态添加到 Promise 数组时,Promise 全部提前完成
- java - 根据杰克逊中的xml文档属性在反序列化期间检测对象类型 - 没有JsonNode
- node.js - 为什么我在 Mongoose 中的 createIndex 失败了?
- c - 为什么“__tracepoint_sched_switch”未定义