Spring + Ehcache - can @Cacheable be used to cache

2019-07-23 09:15发布

问题:

Basically, is it possible to do this:

@Cacheable(cacheName="default")
@RequestMapping("getContent/{name}")
public String getContentByNameHandler(@PathVariable String name, Model model) {

    ContentService contentService = domainService.getContentService();

    model.addAttribute("model",contentService.getContentByName(name));

    return RESOURCE_FOLDER + "content";
}

When I try this, the view is cached, but the only the plain content of the jsp is returned from the cache, not the jsp view after the simple jsp view rendering logic has completed. I'm on spring 3.0.7, so still using the ehcache-spring-annotations (http://code.google.com/p/ehcache-spring-annotations)

回答1:

@Cacheable works by simply forming a key based on all input parameters, and putting the return value under that key.

So it won't store the processed view - it will simply store the view name.

Normally, you'd use browser caching for that instead of server-side caching. And since rendering the view is supposed to be less consuming than generating the content, you'd put @Cacheable on the service method.