# -*- 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 <%! 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 argument to the <%text filter='h'><%page> or <%text filter='h'><%def> directives:

<%call expr="formatting.code()"><%text><%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 <%text filter='h'>render() method will return content directly from the cache. When the <%text filter='h'>Template object itself falls out of scope, its corresponding cache is garbage collected along with the template.

Caching requires that the <%text filter='h'>beaker package be installed on the system.

The caching flag and all its options can be used with the <%text filter='h'><%def> tag.

<%call expr="formatting.code()"><%text><%def name="mycomp" cached="True" cache_timeout="30" cache_type="memory"> other text <%call expr="formatting.section(path='caching_arguments',paged=paged,extension=extension,toc=toc)">

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

The options available are:

<%call expr="formatting.section(path='caching_accessing',paged=paged,extension=extension,toc=toc)">

The <%text filter='h'>Template, as well as any template-derived namespace, has an accessor called <%text filter='h'>cache which returns the <%text filter='h'>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:

<%call expr="formatting.code()"><%text><% local.cache.put("somekey", type="memory", "somevalue") %>

Above, the cache associated with the <%text filter='h'>local namespace is accessed and a key is placed within a memory cache.

More commonly the <%text filter='h'>cache object is used to invalidate cached sections programmatically:

<%call expr="formatting.code(syntaxtype='python')"><%text> 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')