我正在使用Infinipsan在spring-boot项目的一部分中实现缓存。
这就是我所做的:
1.我在project1的spring boot初始化文件中添加了 @EnableCaching 。
2.相应地更新了 application.properties 和 infinispan.xml ,并在project1中创建了一个虚拟天气方法来检查缓存,并且效果很好。
3.然后进入我的实际实现。最初,请求在此处命中project1的控制器
@GetMapping("stream")
public String stream() {
return "stream";
}
@Inject
@Named("faceRecognition")
private FaceRecognition faceRecognition;
@RequestMapping("/frame")
public Object recognize(@RequestBody Map<String, Object> payload) {
return faceRecognition.recognizeFace(payload);
}
@Named("faceRecognition")
public class FaceRecognition {
//implementing the method
@Cacheable(value="testCache", id="#empIdArr")
public ResponseVO elasticSearchDataRet(String empIdArr) {
ResponseVO responseVO = new ResponseVO();
responseVO.setIndex(empIdArr);
responseVO.setType("None");
responseVO.setStatus("Success");
System.out.println("Not cached yet");
return responseVO;
}
public String recognizeFace(Map<String, Object> payload) {
...After doing few things...
//calling the method. Here empIdArr = [2000]
elasticSearchDataRet(empIdArr[0]);
...Do some stuff....
}
}
结果:在检入 localhost:3000 / metrics 时,“ cache.testCache.size”(testCache是我的缓存的名称)为0,并显示“ Not缓存”,即每次发出请求时,即缓存均无法正常工作。
代码有什么问题?
编辑:当我将faceRecognition类更改为如下所示(更改@Cacheable的位置)时,在 localhost:3000 / metrics 中,'cache.testCache.size '会开始递增,尽管每次发出请求时它仍会显示“尚未缓存”:
@Named("faceRecognition")
@Cacheable(value="testCache", id="#empIdArr")
public class FaceRecognition {
//implementing the method
public ResponseVO elasticSearchDataRet(String empIdArr) {
ResponseVO responseVO = new ResponseVO();
responseVO.setIndex(empIdArr);
responseVO.setType("None");
responseVO.setStatus("Success");
System.out.println("Not cached yet");
return responseVO;
}
public String recognizeFace(Map<String, Object> payload) {
...After doing few things...
//calling the method. Here empIdArr = [2000]
elasticSearchDataRet(empIdArr[0]);
...Do some stuff....
}
}