在Tomcat中,可以通過配置web.xml文件來設置緩存策略。以下是一些常見的緩存策略設置:
<web-app>
<filter>
<filter-name>ExpiresFilter</filter-name>
<filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
<init-param>
<param-name>ExpiresByType image</param-name>
<param-value>access plus 1 month</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ExpiresFilter</filter-name>
<url-pattern>*.png</url-pattern>
</filter-mapping>
</web-app>
<web-app>
<filter>
<filter-name>NoCacheFilter</filter-name>
<filter-class>org.apache.catalina.filters.NoCacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>NoCacheFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<web-app>
<filter>
<filter-name>ETagFilter</filter-name>
<filter-class>org.apache.catalina.filters.ETagFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ETagFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
通過以上配置,可以在Tomcat中設置不同的緩存策略來優化網站性能。