简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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