Expert Spring MVC and Web Flow

(Dana P.) #1
If you have set the property cacheSecondsto zero, the following headers will be sent:


  • Pragma: No-cache(universal)

  • Expires: n(where n = current system date + one second, as HTTP-formatted date)
    (HTTP/1.0)

  • Cache-Control: no-cache(HTTP/1.1)

  • Cache-Control: no-store(HTTP/1.1, for Firefox)


If you wish to inform the client to cache the document, the following headers will be sent:


  • Expires: n(where n = current system date + seconds, as HTTP-formatted date)
    (HTTP/1.0)

  • Cache-Control: max-age=s(where s = seconds documents should be cached for)
    (HTTP/1.1)


There is one more element of caching your Controllercan configure, but not through
setting properties. Your Controllerimplementation may also implement the interface
org.springframework.web.servlet.mvc.LastModified(shown in Listing 6-4) to indicate that
it provides a last modified time for the document. This interface contains one method.


Listing 6-4.LastModified Interface


package org.springframework.web.servlet.mvc;
public interface LastModified {
long getLastModified(HttpServletRequest request);
}


If your Controllerimplements this (which AbstractControllerdoes notby default), then
the cache control code can send an extra hint to the client via cache headers. By providing a last
modified date, the Cache-Control header will contain the value max-age=s, must-revalidate,
which instructs the client that it must revalidate the document once it has become stale in
the cache.
The most common way a client will revalidate the document is to issue a conditional request
using the Last-Modified header inside the HTTP request. When the client requests the document
again, it will send a Last-Modified header set to the date when it last received the document. The
document is considered valid if it has not been modified since this date.


■Tip For more on how a client may revalidate a resource, consult section 13.3 of the HTTP RFC,
http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.


As you can see, by implementing LastModified, your Controllerwill be able to accurately
tell the server when its contents last changed. Thus, AbstractControlleris able to incorporate
the must-revalidateinstruction in its Cache-Control headers.


CHAPTER 6 ■THE CONTROLLER MENAGERIE 121
Free download pdf