Cache Content

by gerrykirk — last modified Apr 16, 2009 01:24 PM
How to change Plone's default cache settings so you can cache pages on your site.

Caching Overview

Andy McKay gave an excellent presentation on this topic at the first Plone conference. Read his presentation notes

Some other resources worth reading:

Plone Modifications

Plone's default cache settings do not cache pages. Use the following information to change the settings and let your site cache pages. You need to modify the global_cache_settings file in the plone_templates directory:

  <metal:block tal:define="dummy python:request.RESPONSE.setHeader('Content-Type','text/html;;charset=%s' % charset)" />

  <metal:block tal:define="dummy python:request.RESPONSE.setHeader('Content-Language', lang)" />

  <metal:block tal:define="dummy python:request.RESPONSE.setHeader('Vary', 'Accept-Language,Accept-Encoding,User-Agent,Cookie')" />

  <metal:block tal:define="dummy python:request.RESPONSE.setHeader('Pragma', 'no-cache')" />

  <metal:block tal:define="dummy python:request.RESPONSE.setHeader('Connection', 'keep-alive')" />

  <metal:block tal:define="dummy python:request.RESPONSE.setHeader('Last-Modified', 'here.modified().toZone('GMT').rfc822())" />

Explanation of code above

Vary setting
tells upstream caches to cache by language, encoding, browser and cookie. Note that Internet Explorer requires a page in different languages to have a distinct url for each language, even when language value is different here.
Pragma : no-cache setting
tells browsers to not cache the content. The content should be cached only by Apache, Squid or some other cache on the server, for optimal cache control.
Connection : keep-alive setting
allows persistent connections which can speedup latency times for HTML documents with lots of images.
LastModified header
reports the actual last modified time of the object to an upstream cache, which makes sense for caching.

Zope Modifications

If you are going to use one or more HTTP Accelerated Cache Managers, you need to fix a bug in Zope's AcceleratedHTTPCacheManager.py file. Uncomment the following line:

    RESPONSE.setHeader('Last-Modified',rfc1123_date(time.time()))

This will ensure that Zope returns a valid Last-Modified HTTP header. Without it, caches will assume that your content has been dynamically generated and is not suitable for caching.