简体   繁体   English

通过在Spring-MVC Hibernate应用程序中进行缓存来缩短页面加载时间

[英]Improve page load time by caching in Spring-MVC Hibernate application

I have created a Spring MVC Hibernate application. 我创建了一个Spring MVC Hibernate应用程序。 I am already using Hibernate's 2nd level EhCache to improve performance. 我已经在使用Hibernate的2级EhCache来提高性能。

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. 每2个月大概说一次。

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? 如果是这样,我是否需要在所有控制器方法中添加以上代码以启用所有JSP的缓存?
  3. If not, what is best way to cache the web page? 如果不是,什么是缓存网页的最佳方法?

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. 看起来您的意思是在这里浏览器缓存-如果是这样,那么您的代码所隐含的含义是,发送对/products GET请求的响应带有标头,该标头指示浏览器在特定时期内缓存响应。 Now all the GET requests ( ajax or non-ajax ) from browser should be served from the browser cache. 现在,应该从浏览器缓存中处理来自浏览器的所有GET请求(ajax或non-ajax)。 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 eg, if you are using JSP as view template then something similar to this technique can be used. 例如,如果您使用JSP作为视图模板,则可以使用类似于此技术的东西。 In this case the caching is happening entirely on server and is helpful while aggregating response from static and dynamic parts of response. 在这种情况下,缓存将完全在服务器上进行,这有助于聚合响应的静态和动态部分。

You can also use ETag, check out this filter. 您也可以使用ETag,签出过滤器。

Since the ETag is based on the response content, the response (eg a View) is still rendered. 由于ETag基于响应内容,因此仍会呈现响应(例如View)。 As such, this filter only saves bandwidth, not server performance. 因此,此筛选器仅节省带宽,而不节省服务器性能。

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

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