Multiple Pages | One Page

Mako Documentation

Version: 0.3.4 Last Updated: 06/22/10 17:39:23

Table of Contents


Caching

Caching

Any template or component can be cached using the cache argument to the <%page> or <%def> directives:

<%page cached="True"/>

template text

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 render() method will return content directly from the cache. When the Template object itself falls out of scope, its corresponding cache is garbage collected along with the template.

Caching requires that the beaker package be installed on the system.

The caching flag and all its options can be used with the <%def> tag.

<%def name="mycomp" cached="True" cache_timeout="30" cache_type="memory">
    other text
</%def>

Cache arguments

The various cache arguments are cascaded from their default values, to the arguments specified programmatically to the Template or its originating TemplateLookup, then to those defined in the <%page> tag of an individual template, and finally to an individual <%def> tag within the template. This means you can define, for example, a cache type of dbm on your TemplateLookup, a cache timeout of 60 seconds in a particular template's <%page> tag, and within one of that template's <%def> tags cache=True, and that one particular def will then cache its data using a dbm cache and a data timeout of 60 seconds.

The options available are:

back to section top

Accessing the Cache

The Template, as well as any template-derived namespace, has an accessor called cache which returns the Cache 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:

<%
    local.cache.put("somekey", type="memory", "somevalue")
%>

Above, the cache associated with the local namespace is accessed and a key is placed within a memory cache.

More commonly the cache object is used to invalidate cached sections programmatically:

template = lookup.get_template('/sometemplate.html')

# invalidate the "body" of the template
template.cache.invalidate_body()

# invalidate an individual def
template.cache.invalidate_def('somedef')

# invalidate an arbitrary key
template.cache.invalidate('somekey')
back to section top
Previous: The Unicode Chapter

Table of Contents