The browser cache can be customized by the server side by using the Last-Modified, CacheControl (or Expires with http 1.0) and ETag http headers.
Details about how these headers work can be found here.
CacheControl is the class that handles all the internals related to caching:
Almost all Resolution classes support the CacheControl attribute:
public Resolution foo() {
CacheControl c = new CacheControl();
c.setLastModified(new Date());
c.setCacheSeconds(2000);
return forward("bar.jsp").setCacheControl(c);
}
When using the lastModified attribute keep in mind that the last modification date will be strictly compared (==) instead of using 'less or equals' (<=).
When caching, you may use a weak indicator like lastModified, a strong indicator like etag, or a combination of the two. If the etag includes information about the last modification timestamp, you can skip the Last-Modified header.
You can use the MD5 hash of your response (if known in advance, for example with binary files) as a GET parameter and mark the resource to get cached forever. This should be done only if the resource will not be modified frequently, otherwise the browser cache will easily get populated with garbage.
public class FileAction extends AbstractAction {
/** if present, use this MD5 to cache resources in the browser forever */
private String md5;
@Injected
private FileManager fileManager;
@RequiredValidation(on="get*")
private Integer id;
private PersistentFile file;
@Cache(on="getFile")
public CacheControl getCacheControl() {
file = fileManager.find(id);
CacheControl c = new CacheControl();
c.setEtag(file.getMD5());
if (file.getMD5().equals(md5)) {
c.setCacheForever();
}
return c;
}
public Resolution getFile() {
return send(file);
}
}