3
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
4
<title>Mako Documentation - Caching</title>
6
<link rel="stylesheet" href="docs.css"></link>
7
<link rel="stylesheet" href="highlight.css"></link>
25
<div id="topanchor"><a name="top"> </a></div>
27
<div id="pagecontrol"><a href="index.html">Multiple Pages</a> | <a href="documentation.html">One Page</a></div>
29
<h1>Mako Documentation</h1>
31
<div class="versionheader">Version: 0.1.9 Last Updated: 09/25/07 16:19:18</div>
46
<div class="prevnext">
47
Previous: <a href="unicode.html">The Unicode Chapter</a>
50
<h3><a href="index.html">Table of Contents</a></h3>
55
<a href="#caching">Caching</a>
59
<li><A style="" href="caching.html#caching_arguments">Cache arguments</a></li>
80
<A name="caching"></a>
89
<p>Any template or component can be cached using the <code>cache</code> argument to the <code><%page></code> or <code><%def></code> directives:
96
<div class="highlight" ><pre><span class="cp"><%</span><span class="nb">page</span> <span class="na">cached=</span><span class="s">"True"</span><span class="cp">/></span>
102
<p>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 <code>render()</code> method will return content directly from the cache. When the <code>Template</code> object itself falls out of scope, its corresponding cache is garbage collected along with the template.
104
<p>Caching requires that the <code>beaker</code> package be installed on the system.
106
<p>The caching flag and all its options can be used with the <code><%def></code> tag. <br></br>
113
<div class="highlight" ><pre><span class="cp"><%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">"mycomp"</span> <span class="na">cached=</span><span class="s">"True"</span> <span class="na">cache_timeout=</span><span class="s">"30"</span> <span class="na">cache_type=</span><span class="s">"memory"</span><span class="cp">></span>
115
<span class="cp"></%</span><span class="nb">def</span><span class="cp">></span>
122
<A name="caching_arguments"></a>
124
<div class="subsection">
127
<h3>Cache arguments</h3>
131
<p>The various cache arguments are cascaded from their default values, to the arguments specified programmatically to the <code>Template</code> or its originating <code>TemplateLookup</code>, then to those defined in the <code><%page></code> tag of an individual template, and finally to an individual <code><%def></code> tag within the template. This means you can define, for example, a cache type of <code>dbm</code> on your <code>TemplateLookup</code>, a cache timeout of 60 seconds in a particular template's <code><%page></code> tag, and within one of that template's <code><%def></code> tags <code>cache=True</code>, and that one particular def will then cache its data using a <code>dbm</code> cache and a data timeout of 60 seconds.
133
<p>The options available are:
136
<li><p>cached="False|True" - turn caching on
141
<li><p>cache_timeout - number of seconds in which to invalidate the cached data. after this timeout, the content is re-generated on the next call.
146
<li><p>cache_type - type of caching. <code>memory</code>, <code>file</code>, <code>dbm</code>, or <code>memcached</code>. (TODO: describe extra arguments for memcached)
151
<li><p>cache_dir - In the case of the <code>file</code> and <code>dbm</code> cache types, this is the filesystem directory with which to store data files. If this option is not present, the value of <code>module_directory</code> is used (i.e. the directory where compiled template modules are stored). If neither option is available an exception is thrown.
156
<li><p>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:
163
<div class="highlight" ><pre><span class="cp"><%</span><span class="nb">page</span> <span class="na">cached=</span><span class="s">"True"</span> <span class="na">cache_key=</span><span class="s">"${self.filename}"</span><span class="cp">/></span>
165
<span class="cp">${</span><span class="n">next</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span>
167
<span class="cp">## rest of template</span>
183
<a href="#top">back to section top</a>
190
<div class="toolbar">
191
<div class="prevnext">
192
Previous: <a href="unicode.html">The Unicode Chapter</a>
195
<h3><a href="index.html">Table of Contents</a></h3>