I have created a Spring MVC Hibernate application. I am already using Hibernate's 2nd level EhCache to improve performance.
I want to cache my web page so that when a user visits the page a second time, it loads the page more quickly from the cache. The page contents will not change often; say roughly once every 2 months.
I am using the code below to cache my entire page:
@RequestMapping(value = "/products", method = RequestMethod.GET)
public String getAllProducts(ModelMap model,HttpServletRequest request,HttpServletResponse response ) {
model.addAttribute("products", "all products from backend");
// caching the page
response.addHeader("Cache-Control","max-age="+CACHE_DURATION_IN_SECOND);//1
response.addHeader("Cache-Control", "must-revalidate");//2
response.setDateHeader("Last-Modified", now);//3
response.setDateHeader("Expires", now + CACHE_DURATION_IN_MS);//4
return "all-products";
}
My questions are:
- Is the above code enough to handle caching of a web page?
- If so, do I need to add the above code in all my controller methods to enable caching of all the JSPs?
- If not, what is best way to cache the web page?