我在堆栈上阅读了与此类似的所有旧问题,但我仍然无法得到解决方案。 我试图在spring 4.x中使用@Cacheable,但是我没有从缓存中得到结果: - 我的代码是:
Application.java
@SpringBootApplication
@EnableCaching
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}
}
控制器:
@RestController
@RequestMapping("/test")
public class BootController {
@Autowired
public BootService oBootService;
@RequestMapping("/list{listing_id}")
public ResponseObj getprodList(@RequestParam("listing_id") Long listing_id){
ResponseObj responseObj=new ResponseObj();
List<BootModel> list =this.oBootService.getprodList(listing_id);
responseObj.setObject(list);
if(list.size()>0){
responseObj.setMessage("success");
responseObj.setStatus("200");
responseObj.setStatus_code(200);
}else{
responseObj.setMessage("success");
responseObj.setStatus("204");
responseObj.setStatus_code(204);
}
return responseObj;
}
服务类:
@Service
public class BootService {
@Inject
BootRepo oBootRepo;
@Cacheable(value="prod",key="#listing_id",condition = "#listing_id < 3")
public List<BootModel> getprodList(Long listing_id)
{
List <BootModel> list = oBootRepo.getProdList(listing_id);
return list;
}
当我试图点击url:localhost:8080 / prod / test / list?listing_id = 10 正确的结果来了,但当我再次点击URL&amp;然后再次触发查询以获取数据。 而不是当用户点击查询时,结果应该从缓存而不是从DB显示。请告诉我哪里错了?