# -*- coding: utf-8 -*- <%inherit file="content_layout.html"/> <%page args="toc, extension, paged"/> <%namespace name="formatting" file="formatting.html"/> <%namespace name="nav" file="nav.html"/> <%def name="title()">Mako Documentation - Caching%def> <%! filename = 'caching' %> ## This file is generated. Edit the .txt files instead of this one. <%call expr="formatting.section(path='caching',paged=paged,extension=extension,toc=toc)">
Any template or component can be cached using the <%text filter='h'>cache%text>
argument to the <%text filter='h'><%page>%text>
or <%text filter='h'><%def>%text>
directives:
The above template, after being executed the first time, will store its content within a cache that by default is scoped within memory. Subsequent calls to the template's <%text filter='h'>render()%text>
method will return content directly from the cache. When the <%text filter='h'>Template%text>
object itself falls out of scope, its corresponding cache is garbage collected along with the template.
Caching requires that the <%text filter='h'>beaker%text>
package be installed on the system.
The caching flag and all its options can be used with the <%text filter='h'><%def>%text>
tag.
The various cache arguments are cascaded from their default values, to the arguments specified programmatically to the <%text filter='h'>Template%text>
or its originating <%text filter='h'>TemplateLookup%text>
, then to those defined in the <%text filter='h'><%page>%text>
tag of an individual template, and finally to an individual <%text filter='h'><%def>%text>
tag within the template. This means you can define, for example, a cache type of <%text filter='h'>dbm%text>
on your <%text filter='h'>TemplateLookup%text>
, a cache timeout of 60 seconds in a particular template's <%text filter='h'><%page>%text>
tag, and within one of that template's <%text filter='h'><%def>%text>
tags <%text filter='h'>cache=True%text>
, and that one particular def will then cache its data using a <%text filter='h'>dbm%text>
cache and a data timeout of 60 seconds.
The options available are:
cached="False|True" - turn caching on
cache_timeout - number of seconds in which to invalidate the cached data. after this timeout, the content is re-generated on the next call.
cache_type - type of caching. <%text filter='h'>memory%text>
, <%text filter='h'>file%text>
, <%text filter='h'>dbm%text>
, or <%text filter='h'>memcached%text>
.
cache_url - (only used for <%text filter='h'>memcached%text>
but required) a single IP address or a semi-colon separated list of IP address of memcache servers to use.
cache_dir - In the case of the <%text filter='h'>file%text>
and <%text filter='h'>dbm%text>
cache types, this is the filesystem directory with which to store data files. If this option is not present, the value of <%text filter='h'>module_directory%text>
is used (i.e. the directory where compiled template modules are stored). If neither option is available an exception is thrown.
In the case of the <%text filter='h'>memcached%text>
type, this attribute is required and it's used to store the lock files.
cache_key - the "key" used to uniquely identify this content in the cache. the total namespace of keys within the cache is local to the current template, and the default value of "key" is the name of the def which is storing its data. It is an evaluable tag, so you can put a Python expression to calculate the value of the key on the fly. For example, heres a page that caches any page which inherits from it, based on the filename of the calling template:
<%call expr="formatting.code()"><%text><%page cached="True" cache_key="${self.filename}"/> ${next.body()} ## rest of template %text>%call>The <%text filter='h'>Template%text>
, as well as any template-derived namespace, has an accessor called <%text filter='h'>cache%text>
which returns the <%text filter='h'>Cache%text>
object for that template. This object is a facade on top of the Beaker internal cache object, and provides some very rudimental capabilities, such as the ability to get and put arbitrary values:
Above, the cache associated with the <%text filter='h'>local%text>
namespace is accessed and a key is placed within a memory cache.
More commonly the <%text filter='h'>cache%text>
object is used to invalidate cached sections programmatically: