Set Caching Headers
Shows how to force caching of a page |
Type | Snippet |
Category | |
Developed by | |
Rating | |
License | Apache License 2.0 |
Table of contents
Description
The default XWiki serves everything with no caching allowance, this guarantees that the received in web browsers are always up-to-date but costs the price of refetching the resource and re-processing it. In some cases, the application developers may be sure that allowing the browsers to cache the page for some time is acceptable, for this, a few headers need to be adjusted.
Here is how to set the necessary header for a page to be cached.
The example below displays to clients and proxies the allowance to cache for 60 seconds the content delivered by the current page. It should be put before anything is output in the velocity code.
$response.setHeader("Pragma","-")
#set($expires = $util.getDate().getTime())
#set($expires = $expires+60000)
#set($expires =$util.getDate($expires))
$response.setDateHeader("Expires",$expires.getTime())
Caching and the web is a broad topic, more information can be read at Marc Nottingham's note on web caching and references mentioned there.
Alternatives
Note that these alternatives avoid the script execution but still transfer data from the server to the client
- The Cache Macro
- The Rendering system also has the ability to cache rendered pages