~jonas-drange/ubuntu-start-page/1252899-mobile-friendly

« back to all changes in this revision

Viewing changes to src/Mako-0.1.9/doc/caching.html

  • Committer: Matthew Nuzum
  • Date: 2008-04-18 01:58:53 UTC
  • Revision ID: matthew.nuzum@canonical.com-20080418015853-2b8rf979z2c2exxl
adding files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html>
 
2
<head>
 
3
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 
4
        <title>Mako Documentation - Caching</title>
 
5
        
 
6
    <link rel="stylesheet" href="docs.css"></link>
 
7
    <link rel="stylesheet" href="highlight.css"></link>
 
8
    
 
9
 
 
10
 
 
11
 
 
12
 
 
13
</head>
 
14
<body>
 
15
 
 
16
 
 
17
 
 
18
 
 
19
 
 
20
 
 
21
 
 
22
 
 
23
 
 
24
 
 
25
<div id="topanchor"><a name="top">&nbsp;</a></div>
 
26
 
 
27
<div id="pagecontrol"><a href="index.html">Multiple Pages</a> | <a href="documentation.html">One Page</a></div>
 
28
 
 
29
<h1>Mako Documentation</h1>
 
30
 
 
31
<div class="versionheader">Version: 0.1.9   Last Updated: 09/25/07 16:19:18</div>
 
32
 
 
33
 
 
34
 
 
35
 
 
36
 
 
37
 
 
38
 
 
39
<A name=""></a>
 
40
 
 
41
 
 
42
    <div class="topnav">
 
43
 
 
44
    
 
45
    <div class="toolbar">
 
46
        <div class="prevnext">
 
47
            Previous: <a href="unicode.html">The Unicode Chapter</a>
 
48
 
 
49
        </div>
 
50
        <h3><a href="index.html">Table of Contents</a></h3>
 
51
    </div>
 
52
 
 
53
 
 
54
    <br/>
 
55
        <a href="#caching">Caching</a>
 
56
        
 
57
        
 
58
    <ul>
 
59
        <li><A style="" href="caching.html#caching_arguments">Cache arguments</a></li>
 
60
        
 
61
            
 
62
    <ul>
 
63
    </ul>
 
64
 
 
65
    </ul>
 
66
 
 
67
        </div>
 
68
 
 
69
 
 
70
 
 
71
 
 
72
 
 
73
 
 
74
 
 
75
 
 
76
 
 
77
 
 
78
 
 
79
    
 
80
    <A name="caching"></a>
 
81
    
 
82
    <div class="section">
 
83
    
 
84
 
 
85
    <h3>Caching</h3>
 
86
    
 
87
    
 
88
 
 
89
<p>Any template or component can be cached using the <code>cache</code> argument to the <code>&lt;%page&gt;</code> or <code>&lt;%def&gt;</code> directives:
 
90
</p>
 
91
 
 
92
    
 
93
    
 
94
 
 
95
    <div class="code">
 
96
        <div class="highlight" ><pre><span class="cp">&lt;%</span><span class="nb">page</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span><span class="cp">/&gt;</span>
 
97
 
 
98
template text
 
99
</pre></div>
 
100
 
 
101
    </div>
 
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.
 
103
</p>
 
104
<p>Caching requires that the <code>beaker</code> package be installed on the system.
 
105
</p>
 
106
<p>The caching flag and all its options can be used with the <code>&lt;%def&gt;</code> tag.  <br></br>
 
107
</p>
 
108
 
 
109
    
 
110
    
 
111
 
 
112
    <div class="code">
 
113
        <div class="highlight" ><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;mycomp&quot;</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_timeout=</span><span class="s">&quot;30&quot;</span> <span class="na">cache_type=</span><span class="s">&quot;memory&quot;</span><span class="cp">&gt;</span>
 
114
    other text
 
115
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>
 
116
</pre></div>
 
117
 
 
118
    </div>
 
119
 
 
120
 
 
121
    
 
122
    <A name="caching_arguments"></a>
 
123
    
 
124
    <div class="subsection">
 
125
    
 
126
 
 
127
    <h3>Cache arguments</h3>
 
128
    
 
129
    
 
130
 
 
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>&lt;%page&gt;</code> tag of an individual template, and finally to an individual <code>&lt;%def&gt;</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>&lt;%page&gt;</code> tag, and within one of that template's <code>&lt;%def&gt;</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.
 
132
</p>
 
133
<p>The options available are:
 
134
</p>
 
135
<ul>
 
136
 <li><p>cached="False|True" - turn caching on
 
137
</p>
 
138
 
 
139
 </li>
 
140
 
 
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.
 
142
</p>
 
143
 
 
144
 </li>
 
145
 
 
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)
 
147
</p>
 
148
 
 
149
 </li>
 
150
 
 
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.
 
152
</p>
 
153
 
 
154
 </li>
 
155
 
 
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:
 
157
</p>
 
158
 
 
159
    
 
160
    
 
161
 
 
162
    <div class="code">
 
163
        <div class="highlight" ><pre><span class="cp">&lt;%</span><span class="nb">page</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_key=</span><span class="s">&quot;${self.filename}&quot;</span><span class="cp">/&gt;</span>
 
164
 
 
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>
 
166
 
 
167
<span class="cp">## rest of template</span>
 
168
</pre></div>
 
169
 
 
170
    </div>
 
171
 
 
172
 </li>
 
173
</ul>
 
174
 
 
175
 
 
176
 
 
177
 
 
178
    </div>
 
179
 
 
180
 
 
181
 
 
182
 
 
183
            <a href="#top">back to section top</a>
 
184
    </div>
 
185
 
 
186
 
 
187
</html>
 
188
 
 
189
 
 
190
    <div class="toolbar">
 
191
        <div class="prevnext">
 
192
            Previous: <a href="unicode.html">The Unicode Chapter</a>
 
193
 
 
194
        </div>
 
195
        <h3><a href="index.html">Table of Contents</a></h3>
 
196
    </div>
 
197
 
 
198
 
 
199
 
 
200
 
 
201
 
 
202
 
 
203
</body>
 
204
</html>
 
205
 
 
206
 
 
207
 
 
208
 
 
209