简体   繁体   English

Spring4中的控制器缓存不起作用

[英]Controller cache in Spring4 is not working

When I write the following code the cache doesn't work 当我编写以下代码时,缓存不起作用

@Cacheable("books")
@RequestMapping(method = RequestMethod.GET)
public String list(Model model) {
    System.out.println("Retrieving products");
    model.addAttribute("products", productDao.list());
    return "products/list";
}

But if I write the following the cache works 但是,如果我写以下内容,则缓存有效

@Cacheable("books")
@RequestMapping(method = RequestMethod.GET)
public ModelAndView list() {
    ModelAndView modelAndView = new ModelAndView("products/list");
    System.out.println("Retrieving products");
    modelAndView.addObject("products", productDao.list());
    return modelAndView;
}

Can someone tell why the first code doesn't cache? 有人可以说出为什么第一个代码不缓存吗?

I guess I found the answer. 我想我找到了答案。 Due to the argument, as the object can be different each time it's called, most of the time the value will be not equal. 由于存在该参数,由于每次调用对象都可以不同,因此大多数情况下该值将不相等。

Therefore, the cache will understand it as a different request and will not bring the cached value. 因此,缓存会将其理解为不同的请求,并且不会带来缓存的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM