cogShows how to force caching of a page
TypeSnippet
Category
Developed by

Paul Libbrecht

Rating
0 Votes
LicenseApache 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("Cache-Control","max-age=60, public")
$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

Tags: cache
     

Get Connected