1

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:

  1. Is the above code enough to handle caching of a web page?
  2. If so, do I need to add the above code in all my controller methods to enable caching of all the JSPs?
  3. If not, what is best way to cache the web page?
riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
Peter
  • 185
  • 6
  • 21
  • Is not your browser caching your pages ? Did you check that without this code ? – Viswanath Lekshmanan Oct 16 '15 at 12:08
  • @ViswanathLekshmanan : No. You want to say we don't have to do anything special for caching the web page, the browser will take care automatically (when to expire, revalidate etc.)? – Peter Oct 16 '15 at 12:12
  • Automatically your browser caches the HTML,JS,CSS pages. We restrict browsers when we dont need to cache pages by using the headers.Please check the same – Viswanath Lekshmanan Oct 16 '15 at 12:16

2 Answers2

1

Furthermore I want to cache my web page so that when user visits the page second time, It loads the page more quickly from cache.

Looks like you mean here the browser cache - if that is so then what your code implies that a response for GET request to /products is sent with headers which instructs browsers to cache the response for specific period. Now all the GET requests ( ajax or non-ajax ) from browser should be served from the browser cache. If that is you objective then it is fine. However there is another kind of caching possible ( which is typically used for infrequently changing page fragments) - where the response is stored in server side cache. For e.g., if you are using JSP as view template then something similar to this technique can be used. In this case the caching is happening entirely on server and is helpful while aggregating response from static and dynamic parts of response.

Community
  • 1
  • 1
Shailendra
  • 8,874
  • 2
  • 28
  • 37
  • Your understanding is correct. Is there any other way except Cache taglib (the above you said), I don't want to change my jsp code ? – Peter Oct 16 '15 at 13:28
1

You can also use ETag, check out this filter.

Since the ETag is based on the response content, the response (e.g. a View) is still rendered. As such, this filter only saves bandwidth, not server performance.

gabrielgiussi
  • 9,245
  • 7
  • 41
  • 71