-
Notifications
You must be signed in to change notification settings - Fork 14
CacheFilter
Michael Sanford edited this page May 18, 2017
·
6 revisions
CacheFilter
is a Java Servlet filter that allows you to enable browser caching for requested resources.
Option | Required | Default | Since | Description |
---|---|---|---|---|
expiration |
Yes | -- | 2.2.0 | Cache directive to set an expiration time, in seconds, relative to the current date. Used for both Cache-Control and Expires HTTP headers. |
private |
No | false |
2.0.0 | Cache directive to control where the response may be cached.
|
must-revalidate |
No | false |
2.2.0 | Cache directive to define whether conditional requests(1) are required or not for stale responses. When the must-revalidate directive is present in a response received by a cache, that cache MUST NOT use the entry after it becomes stale to respond to a subsequent request without first revalidating it with the origin server. |
vary |
No | -- | 2.3.0 | Cache directive to instruct proxies to cache different versions of the same resource based on specific request-header fields. Some possible Vary header values include:
Vary header it may result in cache corruption: Vary Header for RESTful Applications. |
(1) If a component is already in the browser's cache and is being re-requested, the browser will pass the Last-Modified
date in the request header. This is called a conditional GET request and if the component has not been modified, the server will return a 304 Not Modified
response.
Declare the filter in your web descriptor file web.xml
:
<filter>
<filter-name>imagesCache</filter-name>
<filter-class>com.samaxes.filter.CacheFilter</filter-class>
<init-param>
<param-name>expiration</param-name>
<param-value>2592000</param-value>
</init-param>
</filter>
<filter>
<filter-name>cssCache</filter-name>
<filter-class>com.samaxes.filter.CacheFilter</filter-class>
<init-param>
<param-name>expiration</param-name>
<param-value>604800</param-value>
</init-param>
<init-param>
<param-name>vary</param-name>
<param-value>Accept-Encoding</param-value>
</init-param>
</filter>
<filter>
<filter-name>jsCache</filter-name>
<filter-class>com.samaxes.filter.CacheFilter</filter-class>
<init-param>
<param-name>expiration</param-name>
<param-value>216000</param-value>
</init-param>
<init-param>
<param-name>private</param-name>
<param-value>true</param-value>
</init-param>
</filter>
Map the filter to serve your static resources:
<filter-mapping>
<filter-name>imagesCache</filter-name>
<url-pattern>/img/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>cssCache</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>jsCache</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>