简体   繁体   English

在Tomcat和Spring MVC中进行缓存

[英]Caching in Tomcat and Spring MVC

I have a web application that gets deployed as war file into a Tomcat container. 我有一个Web应用程序,将它作为War文件部署到Tomcat容器中。 The application itself has two main aspects: 应用程序本身具有两个主要方面:

  1. It uses Spring MVC to provide REST endpoints 它使用Spring MVC提供REST端点
  2. It ships an AngularJS based single page application as static content. 它附带了一个基于AngularJS的单页应用程序作为静态内容。 Technically there is an index.html in the root webapp folder that uses an html http-equiv="refresh" redirect to redirect to the actual index.html in a subfolder. 从技术上讲,根webapp文件夹中有一个index.html ,它使用html http-equiv="refresh"重定向来重定向到子文件夹中的实际index.html。

When I update the application I frequently notice that the browser does not load the latest version from the server and just shows some older cached version. 更新应用程序时,我经常注意到浏览器没有从服务器加载最新版本,而只是显示一些较旧的缓存版本。 For example the website shows the current version number on the login page, so if I update it from 1.0.5 to 1.0.6, I often find that the browser still shows 1.0.5 even if I reload the page. 例如,网站在登录页面上显示当前版本号,因此,如果我将其从1.0.5更新到1.0.6,我经常会发现即使重新加载页面,浏览器仍然显示1.0.5。 Pressing CTRL+F5 to ignore the cache usually solves this temporarily. CTRL+F5忽略缓存通常可以暂时解决此问题。

I am not very familiar with the whole caching topic, so I am looking for resources to get started. 我对整个缓存主题不是很熟悉,因此我正在寻找入门资源。

Questions: 问题:

  • Is the caching problem something that I must configure in Spring? 缓存问题是我必须在Spring中配置的吗?
  • Is it rather something that must be configured in Tomcat when I deploy the war file? 部署war文件时,是否必须在Tomcat中对其进行配置?
  • Is it possible that the problem is caused by the html redirect mentioned above? 问题可能是由上述的html重定向引起的吗?

These tags should help to prevent reading from cache 这些标签应有助于防止从缓存中读取

<meta http-equiv="Pragma" content="no-cache">
 <meta http-equiv="Cache-Control" content="no-cache">
 <meta http-equiv="Expires" content="Sat, 01 Dec 2001 00:00:00 GMT">

The Cache-Control header was added in HTTP 1.1, while the other two were also present in HTTP 1.0. Cache-Control标头是在HTTP 1.1中添加的,而其他两个标头也存在于HTTP 1.0中。

Pressing CTRL+F5 to ignore the cache usually solves this temporarily. 按CTRL + F5忽略缓存通常可以暂时解决此问题。

I believe this means that the whole thing is in browser caching/HTTP caching . 我相信这意味着整个过程都在浏览器缓存/ HTTP缓存中

Solution 1. 解决方案1。

Disable cache in Google Chrome via settings (or find a similar article for your browser). 通过设置禁用Google Chrome浏览器中的缓存 (或为您的浏览器找到类似的文章)。

Solution 2. 解决方案2。

Browser caching is actually controlled by the Cache-Control header. 浏览器缓存实际上是由Cache-Control标头Cache-Control

You can avoid this condition if you tell your browser not to cache the dynamic content. 如果告诉浏览器不要缓存动态内容,则可以避免这种情况。 To do this, you'd have to write some cache-control header. 为此,您必须编写一些缓存控制标头。

Well, in the Java Spring Framework, there is a very easy way to stop dynamic content caching. 嗯,在Java Spring框架中,有一种非常简单的方法来停止动态内容缓存。 In your servlet context, just declare a bean WebContentInterceptor and define its properties. 在servlet上下文中,只需声明一个bean WebContentInterceptor并定义其属性。

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <mvc:exclude-mapping path="/resources/**"/>
        <bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
            <property name="cacheSeconds" value="0"/>
            <property name="useExpiresHeader" value="true"/>
            <property name="useCacheControlHeader" value="true"/>
            <property name="useCacheControlNoStore" value="true"/>
        </bean>
    </mvc:interceptor>
</mvc:interceptors>

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

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