caching.html 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  3. <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
  4. <head>
  5. <title>
  6. Caching
  7. &mdash;
  8. Mako 1.0.7 Documentation
  9. </title>
  10. <!-- begin iterate through sphinx environment css_files -->
  11. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  12. <link rel="stylesheet" href="_static/docs.css" type="text/css" />
  13. <link rel="stylesheet" href="_static/site.css" type="text/css" />
  14. <link rel="stylesheet" href="_static/changelog.css" type="text/css" />
  15. <link rel="stylesheet" href="_static/sphinx_paramlinks.css" type="text/css" />
  16. <!-- end iterate through sphinx environment css_files -->
  17. <script type="text/javascript">
  18. var DOCUMENTATION_OPTIONS = {
  19. URL_ROOT: './',
  20. VERSION: '1.0.7',
  21. COLLAPSE_MODINDEX: false,
  22. FILE_SUFFIX: '.html'
  23. };
  24. </script>
  25. <script type="text/javascript" src="_static/jquery.js"></script>
  26. <script type="text/javascript" src="_static/underscore.js"></script>
  27. <script type="text/javascript" src="_static/doctools.js"></script>
  28. <link rel="index" title="Index" href="genindex.html" />
  29. <link rel="search" title="Search" href="search.html" />
  30. <link rel="top" title="Mako 1.0.7 Documentation" href="index.html" />
  31. <link rel="next" title="Changelog" href="changelog.html" />
  32. <link rel="prev" title="The Unicode Chapter" href="unicode.html" />
  33. </head>
  34. <body>
  35. <div id="wrap">
  36. <div class="rightbar">
  37. <div class="slogan">
  38. Hyperfast and lightweight templating for the Python platform.
  39. </div>
  40. </div>
  41. <a href="http://www.makotemplates.org/"><img src="_static/makoLogo.png" /></a>
  42. <hr/>
  43. <div id="docs-container">
  44. <div id="docs-header">
  45. <h1>Mako 1.0.7 Documentation</h1>
  46. <div id="docs-search">
  47. Search:
  48. <form class="search" action="search.html" method="get">
  49. <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
  50. <input type="hidden" name="check_keywords" value="yes" />
  51. <input type="hidden" name="area" value="default" />
  52. </form>
  53. </div>
  54. <div id="docs-version-header">
  55. Release: <span class="version-num">1.0.7</span>
  56. </div>
  57. </div>
  58. <div id="docs-top-navigation">
  59. <div id="docs-top-page-control" class="docs-navigation-links">
  60. <ul>
  61. <li>Prev:
  62. <a href="unicode.html" title="previous chapter">The Unicode Chapter</a>
  63. </li>
  64. <li>Next:
  65. <a href="changelog.html" title="next chapter">Changelog</a>
  66. </li>
  67. <li>
  68. <a href="index.html">Table of Contents</a> |
  69. <a href="genindex.html">Index</a>
  70. | <a href="_sources/caching.rst.txt">view source
  71. </li>
  72. </ul>
  73. </div>
  74. <div id="docs-navigation-banner">
  75. <a href="index.html">Mako 1.0.7 Documentation</a>
  76. »
  77. Caching
  78. <h2>
  79. Caching
  80. </h2>
  81. </div>
  82. </div>
  83. <div id="docs-body-container">
  84. <div id="docs-sidebar">
  85. <h3><a href="index.html">Table of Contents</a></h3>
  86. <ul>
  87. <li><a class="reference internal" href="#">Caching</a><ul>
  88. <li><a class="reference internal" href="#cache-arguments">Cache Arguments</a><ul>
  89. <li><a class="reference internal" href="#backend-specific-cache-arguments">Backend-Specific Cache Arguments</a></li>
  90. <li><a class="reference internal" href="#using-the-beaker-cache-backend">Using the Beaker Cache Backend</a></li>
  91. <li><a class="reference internal" href="#using-the-dogpile-cache-backend">Using the dogpile.cache Backend</a></li>
  92. </ul>
  93. </li>
  94. <li><a class="reference internal" href="#programmatic-cache-access">Programmatic Cache Access</a></li>
  95. <li><a class="reference internal" href="#cache-plugins">Cache Plugins</a><ul>
  96. <li><a class="reference internal" href="#guidelines-for-writing-cache-plugins">Guidelines for Writing Cache Plugins</a></li>
  97. </ul>
  98. </li>
  99. <li><a class="reference internal" href="#api-reference">API Reference</a></li>
  100. </ul>
  101. </li>
  102. </ul>
  103. <h4>Previous Topic</h4>
  104. <p>
  105. <a href="unicode.html" title="previous chapter">The Unicode Chapter</a>
  106. </p>
  107. <h4>Next Topic</h4>
  108. <p>
  109. <a href="changelog.html" title="next chapter">Changelog</a>
  110. </p>
  111. <h4>Quick Search</h4>
  112. <p>
  113. <form class="search" action="search.html" method="get">
  114. <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
  115. <input type="hidden" name="check_keywords" value="yes" />
  116. <input type="hidden" name="area" value="default" />
  117. </form>
  118. </p>
  119. </div>
  120. <div id="docs-body" class="withsidebar" >
  121. <div class="section" id="caching">
  122. <span id="caching-toplevel"></span><h1>Caching<a class="headerlink" href="#caching" title="Permalink to this headline">¶</a></h1>
  123. <p>Any template or component can be cached using the <code class="docutils literal"><span class="pre">cache</span></code>
  124. argument to the <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code>, <code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code> or <code class="docutils literal"><span class="pre">&lt;%block&gt;</span></code> directives:</p>
  125. <div class="highlight-mako"><div class="highlight"><pre><span></span><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><span class="x"></span>
  126. <span class="x">template text</span>
  127. </pre></div>
  128. </div>
  129. <p>The above template, after being executed the first time, will
  130. store its content within a cache that by default is scoped
  131. within memory. Subsequent calls to the template&#8217;s <a class="reference internal" href="usage.html#mako.template.Template.render" title="mako.template.Template.render"><code class="xref py py-meth docutils literal"><span class="pre">render()</span></code></a>
  132. method will return content directly from the cache. When the
  133. <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> object itself falls out of scope, its corresponding
  134. cache is garbage collected along with the template.</p>
  135. <p>The caching system requires that a cache backend be installed; this
  136. includes either the <a class="reference external" href="http://beaker.readthedocs.org/">Beaker</a> package
  137. or the <a class="reference external" href="http://dogpilecache.readthedocs.org">dogpile.cache</a>, as well as
  138. any other third-party caching libraries that feature Mako integration.</p>
  139. <p>By default, caching will attempt to make use of Beaker.
  140. To use dogpile.cache, the
  141. <code class="docutils literal"><span class="pre">cache_impl</span></code> argument must be set; see this argument in the
  142. section <a class="reference internal" href="#cache-arguments"><span class="std std-ref">Cache Arguments</span></a>.</p>
  143. <p>In addition to being available on the <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code> tag, the caching flag and all
  144. its options can be used with the <code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code> tag as well:</p>
  145. <div class="highlight-mako"><div class="highlight"><pre><span></span><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;60&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  146. <span class="x"> other text</span>
  147. <span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
  148. </pre></div>
  149. </div>
  150. <p>... and equivalently with the <code class="docutils literal"><span class="pre">&lt;%block&gt;</span></code> tag, anonymous or named:</p>
  151. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_timeout=</span><span class="s">&quot;60&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  152. <span class="x"> other text</span>
  153. <span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
  154. </pre></div>
  155. </div>
  156. <div class="section" id="cache-arguments">
  157. <span id="id1"></span><h2>Cache Arguments<a class="headerlink" href="#cache-arguments" title="Permalink to this headline">¶</a></h2>
  158. <p>Mako has two cache arguments available on tags that are
  159. available in all cases. The rest of the arguments
  160. available are specific to a backend.</p>
  161. <p>The two generic tags arguments are:</p>
  162. <ul>
  163. <li><p class="first"><code class="docutils literal"><span class="pre">cached=&quot;True&quot;</span></code> - enable caching for this <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code>,
  164. <code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code>, or <code class="docutils literal"><span class="pre">&lt;%block&gt;</span></code>.</p>
  165. </li>
  166. <li><p class="first"><code class="docutils literal"><span class="pre">cache_key</span></code> - the &#8220;key&#8221; used to uniquely identify this content
  167. in the cache. Usually, this key is chosen automatically
  168. based on the name of the rendering callable (i.e. <code class="docutils literal"><span class="pre">body</span></code>
  169. when used in <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code>, the name of the def when using <code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code>,
  170. the explicit or internally-generated name when using <code class="docutils literal"><span class="pre">&lt;%block&gt;</span></code>).
  171. Using the <code class="docutils literal"><span class="pre">cache_key</span></code> parameter, the key can be overridden
  172. using a fixed or programmatically generated value.</p>
  173. <p>For example, here&#8217;s a page
  174. that caches any page which inherits from it, based on the
  175. filename of the calling template:</p>
  176. <div class="highlight-mako"><div class="highlight"><pre><span></span><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><span class="x"></span>
  177. <span class="cp">${</span><span class="nb">next</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span>
  178. <span class="cp">## rest of template</span><span class="x"></span>
  179. </pre></div>
  180. </div>
  181. </li>
  182. </ul>
  183. <p>On a <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> or <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><code class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></code></a>, the
  184. caching can be configured using these arguments:</p>
  185. <ul>
  186. <li><p class="first"><code class="docutils literal"><span class="pre">cache_enabled</span></code> - Setting this
  187. to <code class="docutils literal"><span class="pre">False</span></code> will disable all caching functionality
  188. when the template renders. Defaults to <code class="docutils literal"><span class="pre">True</span></code>.
  189. e.g.:</p>
  190. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">lookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span>
  191. <span class="n">directories</span><span class="o">=</span><span class="s1">&#39;/path/to/templates&#39;</span><span class="p">,</span>
  192. <span class="n">cache_enabled</span> <span class="o">=</span> <span class="bp">False</span>
  193. <span class="p">)</span>
  194. </pre></div>
  195. </div>
  196. </li>
  197. <li><p class="first"><code class="docutils literal"><span class="pre">cache_impl</span></code> - The string name of the cache backend
  198. to use. This defaults to <code class="docutils literal"><span class="pre">'beaker'</span></code>, indicating
  199. that the &#8216;beaker&#8217; backend will be used.</p>
  200. </li>
  201. <li><p class="first"><code class="docutils literal"><span class="pre">cache_args</span></code> - A dictionary of cache parameters that
  202. will be consumed by the cache backend. See
  203. <a class="reference internal" href="#beaker-backend"><span class="std std-ref">Using the Beaker Cache Backend</span></a> and <a class="reference internal" href="#dogpile-cache-backend"><span class="std std-ref">Using the dogpile.cache Backend</span></a> for examples.</p>
  204. </li>
  205. </ul>
  206. <div class="section" id="backend-specific-cache-arguments">
  207. <h3>Backend-Specific Cache Arguments<a class="headerlink" href="#backend-specific-cache-arguments" title="Permalink to this headline">¶</a></h3>
  208. <p>The <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code>, <code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code>, and <code class="docutils literal"><span class="pre">&lt;%block&gt;</span></code> tags
  209. accept any named argument that starts with the prefix <code class="docutils literal"><span class="pre">&quot;cache_&quot;</span></code>.
  210. Those arguments are then packaged up and passed along to the
  211. underlying caching implementation, minus the <code class="docutils literal"><span class="pre">&quot;cache_&quot;</span></code> prefix.</p>
  212. <p>The actual arguments understood are determined by the backend.</p>
  213. <ul class="simple">
  214. <li><a class="reference internal" href="#beaker-backend"><span class="std std-ref">Using the Beaker Cache Backend</span></a> - Includes arguments understood by
  215. Beaker.</li>
  216. <li><a class="reference internal" href="#dogpile-cache-backend"><span class="std std-ref">Using the dogpile.cache Backend</span></a> - Includes arguments understood by
  217. dogpile.cache.</li>
  218. </ul>
  219. </div>
  220. <div class="section" id="using-the-beaker-cache-backend">
  221. <span id="beaker-backend"></span><h3>Using the Beaker Cache Backend<a class="headerlink" href="#using-the-beaker-cache-backend" title="Permalink to this headline">¶</a></h3>
  222. <p>When using Beaker, new implementations will want to make usage
  223. of <strong>cache regions</strong> so that cache configurations can be maintained
  224. externally to templates. These configurations live under
  225. named &#8220;regions&#8221; that can be referred to within templates themselves.</p>
  226. <div class="versionadded">
  227. <p><span class="versionmodified">New in version 0.6.0: </span>Support for Beaker cache regions.</p>
  228. </div>
  229. <p>For example, suppose we would like two regions. One is a &#8220;short term&#8221;
  230. region that will store content in a memory-based dictionary,
  231. expiring after 60 seconds. The other is a Memcached region,
  232. where values should expire in five minutes. To configure
  233. our <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><code class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></code></a>, first we get a handle to a
  234. <a class="reference external" href="http://beaker.readthedocs.io/en/latest/modules/cache.html#beaker.cache.CacheManager" title="(in Beaker v1.9)"><code class="xref py py-class docutils literal"><span class="pre">beaker.cache.CacheManager</span></code></a>:</p>
  235. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">beaker.cache</span> <span class="kn">import</span> <span class="n">CacheManager</span>
  236. <span class="n">manager</span> <span class="o">=</span> <span class="n">CacheManager</span><span class="p">(</span><span class="n">cache_regions</span><span class="o">=</span><span class="p">{</span>
  237. <span class="s1">&#39;short_term&#39;</span><span class="p">:{</span>
  238. <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;memory&#39;</span><span class="p">,</span>
  239. <span class="s1">&#39;expire&#39;</span><span class="p">:</span> <span class="mi">60</span>
  240. <span class="p">},</span>
  241. <span class="s1">&#39;long_term&#39;</span><span class="p">:{</span>
  242. <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;ext:memcached&#39;</span><span class="p">,</span>
  243. <span class="s1">&#39;url&#39;</span><span class="p">:</span> <span class="s1">&#39;127.0.0.1:11211&#39;</span><span class="p">,</span>
  244. <span class="s1">&#39;expire&#39;</span><span class="p">:</span> <span class="mi">300</span>
  245. <span class="p">}</span>
  246. <span class="p">})</span>
  247. <span class="n">lookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span>
  248. <span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;/path/to/templates&#39;</span><span class="p">],</span>
  249. <span class="n">module_directory</span><span class="o">=</span><span class="s1">&#39;/path/to/modules&#39;</span><span class="p">,</span>
  250. <span class="n">cache_impl</span><span class="o">=</span><span class="s1">&#39;beaker&#39;</span><span class="p">,</span>
  251. <span class="n">cache_args</span><span class="o">=</span><span class="p">{</span>
  252. <span class="s1">&#39;manager&#39;</span><span class="p">:</span><span class="n">manager</span>
  253. <span class="p">}</span>
  254. <span class="p">)</span>
  255. </pre></div>
  256. </div>
  257. <p>Our templates can then opt to cache data in one of either region,
  258. using the <code class="docutils literal"><span class="pre">cache_region</span></code> argument. Such as using <code class="docutils literal"><span class="pre">short_term</span></code>
  259. at the <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code> level:</p>
  260. <div class="highlight-mako"><div class="highlight"><pre><span></span><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_region=</span><span class="s">&quot;short_term&quot;</span><span class="cp">&gt;</span>
  261. <span class="cp">## ...</span><span class="x"></span>
  262. </pre></div>
  263. </div>
  264. <p>Or, <code class="docutils literal"><span class="pre">long_term</span></code> at the <code class="docutils literal"><span class="pre">&lt;%block&gt;</span></code> level:</p>
  265. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">name=</span><span class="s">&quot;header&quot;</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_region=</span><span class="s">&quot;long_term&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  266. <span class="x"> other text</span>
  267. <span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
  268. </pre></div>
  269. </div>
  270. <p>The Beaker backend also works without regions. There are a
  271. variety of arguments that can be passed to the <code class="docutils literal"><span class="pre">cache_args</span></code>
  272. dictionary, which are also allowable in templates via the
  273. <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code>, <code class="docutils literal"><span class="pre">&lt;%block&gt;</span></code>,
  274. and <code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code> tags specific to those sections. The values
  275. given override those specified at the <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><code class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></code></a>
  276. or <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> level.</p>
  277. <p>With the possible exception
  278. of <code class="docutils literal"><span class="pre">cache_timeout</span></code>, these arguments are probably better off
  279. staying at the template configuration level. Each argument
  280. specified as <code class="docutils literal"><span class="pre">cache_XYZ</span></code> in a template tag is specified
  281. without the <code class="docutils literal"><span class="pre">cache_</span></code> prefix in the <code class="docutils literal"><span class="pre">cache_args</span></code> dictionary:</p>
  282. <ul class="simple">
  283. <li><code class="docutils literal"><span class="pre">cache_timeout</span></code> - number of seconds in which to invalidate the
  284. cached data. After this timeout, the content is re-generated
  285. on the next call. Available as <code class="docutils literal"><span class="pre">timeout</span></code> in the <code class="docutils literal"><span class="pre">cache_args</span></code>
  286. dictionary.</li>
  287. <li><code class="docutils literal"><span class="pre">cache_type</span></code> - type of caching. <code class="docutils literal"><span class="pre">'memory'</span></code>, <code class="docutils literal"><span class="pre">'file'</span></code>, <code class="docutils literal"><span class="pre">'dbm'</span></code>, or
  288. <code class="docutils literal"><span class="pre">'ext:memcached'</span></code> (note that the string <code class="docutils literal"><span class="pre">memcached</span></code> is
  289. also accepted by the dogpile.cache Mako plugin, though not by Beaker itself).
  290. Available as <code class="docutils literal"><span class="pre">type</span></code> in the <code class="docutils literal"><span class="pre">cache_args</span></code> dictionary.</li>
  291. <li><code class="docutils literal"><span class="pre">cache_url</span></code> - (only used for <code class="docutils literal"><span class="pre">memcached</span></code> but required) a single
  292. IP address or a semi-colon separated list of IP address of
  293. memcache servers to use. Available as <code class="docutils literal"><span class="pre">url</span></code> in the <code class="docutils literal"><span class="pre">cache_args</span></code>
  294. dictionary.</li>
  295. <li><code class="docutils literal"><span class="pre">cache_dir</span></code> - in the case of the <code class="docutils literal"><span class="pre">'file'</span></code> and <code class="docutils literal"><span class="pre">'dbm'</span></code> cache types,
  296. this is the filesystem directory with which to store data
  297. files. If this option is not present, the value of
  298. <code class="docutils literal"><span class="pre">module_directory</span></code> is used (i.e. the directory where compiled
  299. template modules are stored). If neither option is available
  300. an exception is thrown. Available as <code class="docutils literal"><span class="pre">dir</span></code> in the
  301. <code class="docutils literal"><span class="pre">cache_args</span></code> dictionary.</li>
  302. </ul>
  303. </div>
  304. <div class="section" id="using-the-dogpile-cache-backend">
  305. <span id="dogpile-cache-backend"></span><h3>Using the dogpile.cache Backend<a class="headerlink" href="#using-the-dogpile-cache-backend" title="Permalink to this headline">¶</a></h3>
  306. <p><a class="reference external" href="http://dogpilecache.readthedocs.org">dogpile.cache</a> is a new replacement for Beaker. It provides
  307. a modernized, slimmed down interface and is generally easier to use
  308. than Beaker. As of this writing it has not yet been released. dogpile.cache
  309. includes its own Mako cache plugin &#8211; see <a class="reference external" href="http://dogpilecache.readthedocs.io/en/latest/api.html#module-dogpile.cache.plugins.mako_cache" title="(in dogpile.cache v0.6.5)"><code class="xref py py-mod docutils literal"><span class="pre">dogpile.cache.plugins.mako_cache</span></code></a> in the
  310. dogpile.cache documentation.</p>
  311. </div>
  312. </div>
  313. <div class="section" id="programmatic-cache-access">
  314. <h2>Programmatic Cache Access<a class="headerlink" href="#programmatic-cache-access" title="Permalink to this headline">¶</a></h2>
  315. <p>The <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a>, as well as any template-derived <a class="reference internal" href="namespaces.html#mako.runtime.Namespace" title="mako.runtime.Namespace"><code class="xref py py-class docutils literal"><span class="pre">Namespace</span></code></a>, has
  316. an accessor called <code class="docutils literal"><span class="pre">cache</span></code> which returns the <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><code class="xref py py-class docutils literal"><span class="pre">Cache</span></code></a> object
  317. for that template. This object is a facade on top of the underlying
  318. <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></code></a> object, and provides some very rudimental
  319. capabilities, such as the ability to get and put arbitrary
  320. values:</p>
  321. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span>
  322. <span class="n">local</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">&quot;somekey&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s2">&quot;memory&quot;</span><span class="p">,</span> <span class="s2">&quot;somevalue&quot;</span><span class="p">)</span>
  323. <span class="cp">%&gt;</span><span class="x"></span>
  324. </pre></div>
  325. </div>
  326. <p>Above, the cache associated with the <code class="docutils literal"><span class="pre">local</span></code> namespace is
  327. accessed and a key is placed within a memory cache.</p>
  328. <p>More commonly, the <code class="docutils literal"><span class="pre">cache</span></code> object is used to invalidate cached
  329. sections programmatically:</p>
  330. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">template</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s1">&#39;/sometemplate.html&#39;</span><span class="p">)</span>
  331. <span class="c1"># invalidate the &quot;body&quot; of the template</span>
  332. <span class="n">template</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">invalidate_body</span><span class="p">()</span>
  333. <span class="c1"># invalidate an individual def</span>
  334. <span class="n">template</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">invalidate_def</span><span class="p">(</span><span class="s1">&#39;somedef&#39;</span><span class="p">)</span>
  335. <span class="c1"># invalidate an arbitrary key</span>
  336. <span class="n">template</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">invalidate</span><span class="p">(</span><span class="s1">&#39;somekey&#39;</span><span class="p">)</span>
  337. </pre></div>
  338. </div>
  339. <p>You can access any special method or attribute of the <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></code></a>
  340. itself using the <a class="reference internal" href="#mako.cache.Cache.impl" title="mako.cache.Cache.impl"><code class="xref py py-attr docutils literal"><span class="pre">impl</span></code></a> attribute:</p>
  341. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">template</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">impl</span><span class="o">.</span><span class="n">do_something_special</span><span class="p">()</span>
  342. </pre></div>
  343. </div>
  344. <p>Note that using implementation-specific methods will mean you can&#8217;t
  345. swap in a different kind of <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></code></a> implementation at a
  346. later time.</p>
  347. </div>
  348. <div class="section" id="cache-plugins">
  349. <span id="id2"></span><h2>Cache Plugins<a class="headerlink" href="#cache-plugins" title="Permalink to this headline">¶</a></h2>
  350. <p>The mechanism used by caching can be plugged in
  351. using a <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></code></a> subclass. This class implements
  352. the rudimental methods Mako needs to implement the caching
  353. API. Mako includes the <a class="reference internal" href="#mako.ext.beaker_cache.BeakerCacheImpl" title="mako.ext.beaker_cache.BeakerCacheImpl"><code class="xref py py-class docutils literal"><span class="pre">BeakerCacheImpl</span></code></a> class to
  354. provide the default implementation. A <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></code></a> class
  355. is acquired by Mako using a <code class="docutils literal"><span class="pre">pkg_resources</span></code> entrypoint, using
  356. the name given as the <code class="docutils literal"><span class="pre">cache_impl</span></code> argument to <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a>
  357. or <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><code class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></code></a>. This entry point can be
  358. installed via the standard <cite>setuptools</cite>/<code class="docutils literal"><span class="pre">setup()</span></code> procedure, underneath
  359. the <cite>EntryPoint</cite> group named <code class="docutils literal"><span class="pre">&quot;mako.cache&quot;</span></code>. It can also be
  360. installed at runtime via a convenience installer <a class="reference internal" href="#mako.cache.register_plugin" title="mako.cache.register_plugin"><code class="xref py py-func docutils literal"><span class="pre">register_plugin()</span></code></a>
  361. which accomplishes essentially the same task.</p>
  362. <p>An example plugin that implements a local dictionary cache:</p>
  363. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">mako.cache</span> <span class="kn">import</span> <span class="n">Cacheimpl</span><span class="p">,</span> <span class="n">register_plugin</span>
  364. <span class="k">class</span> <span class="nc">SimpleCacheImpl</span><span class="p">(</span><span class="n">CacheImpl</span><span class="p">):</span>
  365. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache</span><span class="p">):</span>
  366. <span class="nb">super</span><span class="p">(</span><span class="n">SimpleCacheImpl</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">cache</span><span class="p">)</span>
  367. <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span> <span class="o">=</span> <span class="p">{}</span>
  368. <span class="k">def</span> <span class="nf">get_or_create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">creation_function</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
  369. <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="p">:</span>
  370. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
  371. <span class="k">else</span><span class="p">:</span>
  372. <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span> <span class="o">=</span> <span class="n">creation_function</span><span class="p">()</span>
  373. <span class="k">return</span> <span class="n">value</span>
  374. <span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
  375. <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
  376. <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
  377. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  378. <span class="k">def</span> <span class="nf">invalidate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
  379. <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
  380. <span class="c1"># optional - register the class locally</span>
  381. <span class="n">register_plugin</span><span class="p">(</span><span class="s2">&quot;simple&quot;</span><span class="p">,</span> <span class="vm">__name__</span><span class="p">,</span> <span class="s2">&quot;SimpleCacheImpl&quot;</span><span class="p">)</span>
  382. </pre></div>
  383. </div>
  384. <p>Enabling the above plugin in a template would look like:</p>
  385. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s2">&quot;mytemplate&quot;</span><span class="p">,</span>
  386. <span class="nb">file</span><span class="o">=</span><span class="s2">&quot;mytemplate.html&quot;</span><span class="p">,</span>
  387. <span class="n">cache_impl</span><span class="o">=</span><span class="s1">&#39;simple&#39;</span><span class="p">)</span>
  388. </pre></div>
  389. </div>
  390. <div class="section" id="guidelines-for-writing-cache-plugins">
  391. <h3>Guidelines for Writing Cache Plugins<a class="headerlink" href="#guidelines-for-writing-cache-plugins" title="Permalink to this headline">¶</a></h3>
  392. <ul class="simple">
  393. <li>The <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></code></a> is created on a per-<a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> basis. The
  394. class should ensure that only data for the parent <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> is
  395. persisted or returned by the cache methods. The actual <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a>
  396. is available via the <code class="docutils literal"><span class="pre">self.cache.template</span></code> attribute. The <code class="docutils literal"><span class="pre">self.cache.id</span></code>
  397. attribute, which is essentially the unique modulename of the template, is
  398. a good value to use in order to represent a unique namespace of keys specific
  399. to the template.</li>
  400. <li>Templates only use the <a class="reference internal" href="#mako.cache.CacheImpl.get_or_create" title="mako.cache.CacheImpl.get_or_create"><code class="xref py py-meth docutils literal"><span class="pre">CacheImpl.get_or_create()</span></code></a> method
  401. in an implicit fashion. The <a class="reference internal" href="#mako.cache.CacheImpl.set" title="mako.cache.CacheImpl.set"><code class="xref py py-meth docutils literal"><span class="pre">CacheImpl.set()</span></code></a>,
  402. <a class="reference internal" href="#mako.cache.CacheImpl.get" title="mako.cache.CacheImpl.get"><code class="xref py py-meth docutils literal"><span class="pre">CacheImpl.get()</span></code></a>, and <a class="reference internal" href="#mako.cache.CacheImpl.invalidate" title="mako.cache.CacheImpl.invalidate"><code class="xref py py-meth docutils literal"><span class="pre">CacheImpl.invalidate()</span></code></a> methods are
  403. only used in response to direct programmatic access to the corresponding
  404. methods on the <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><code class="xref py py-class docutils literal"><span class="pre">Cache</span></code></a> object.</li>
  405. <li><a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></code></a> will be accessed in a multithreaded fashion if the
  406. <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> itself is used multithreaded. Care should be taken
  407. to ensure caching implementations are threadsafe.</li>
  408. <li>A library like <a class="reference external" href="http://pypi.python.org/pypi/dogpile.core">Dogpile</a>, which
  409. is a minimal locking system derived from Beaker, can be used to help
  410. implement the <a class="reference internal" href="#mako.cache.CacheImpl.get_or_create" title="mako.cache.CacheImpl.get_or_create"><code class="xref py py-meth docutils literal"><span class="pre">CacheImpl.get_or_create()</span></code></a> method in a threadsafe
  411. way that can maximize effectiveness across multiple threads as well
  412. as processes. <a class="reference internal" href="#mako.cache.CacheImpl.get_or_create" title="mako.cache.CacheImpl.get_or_create"><code class="xref py py-meth docutils literal"><span class="pre">CacheImpl.get_or_create()</span></code></a> is the
  413. key method used by templates.</li>
  414. <li>All arguments passed to <code class="docutils literal"><span class="pre">**kw</span></code> come directly from the parameters
  415. inside the <code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code>, <code class="docutils literal"><span class="pre">&lt;%block&gt;</span></code>, or <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code> tags directly,
  416. minus the <code class="docutils literal"><span class="pre">&quot;cache_&quot;</span></code> prefix, as strings, with the exception of
  417. the argument <code class="docutils literal"><span class="pre">cache_timeout</span></code>, which is passed to the plugin
  418. as the name <code class="docutils literal"><span class="pre">timeout</span></code> with the value converted to an integer.
  419. Arguments present in <code class="docutils literal"><span class="pre">cache_args</span></code> on <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> or
  420. <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><code class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></code></a> are passed directly, but are superseded
  421. by those present in the most specific template tag.</li>
  422. <li>The directory where <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> places module files can
  423. be acquired using the accessor <code class="docutils literal"><span class="pre">self.cache.template.module_directory</span></code>.
  424. This directory can be a good place to throw cache-related work
  425. files, underneath a prefix like <code class="docutils literal"><span class="pre">_my_cache_work</span></code> so that name
  426. conflicts with generated modules don&#8217;t occur.</li>
  427. </ul>
  428. </div>
  429. </div>
  430. <div class="section" id="api-reference">
  431. <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to this headline">¶</a></h2>
  432. <dl class="class">
  433. <dt id="mako.cache.Cache">
  434. <em class="property">class </em><code class="descclassname">mako.cache.</code><code class="descname">Cache</code><span class="sig-paren">(</span><em>template</em>, <em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.Cache" title="Permalink to this definition">¶</a></dt>
  435. <dd><p>Bases: <code class="xref py py-class docutils literal"><span class="pre">object</span></code></p>
  436. <p>Represents a data content cache made available to the module
  437. space of a specific <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> object.</p>
  438. <div class="versionadded">
  439. <p><span class="versionmodified">New in version 0.6: </span><a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><code class="xref py py-class docutils literal"><span class="pre">Cache</span></code></a> by itself is mostly a
  440. container for a <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></code></a> object, which implements
  441. a fixed API to provide caching services; specific subclasses exist to
  442. implement different
  443. caching strategies. Mako includes a backend that works with
  444. the Beaker caching system. Beaker itself then supports
  445. a number of backends (i.e. file, memory, memcached, etc.)</p>
  446. </div>
  447. <p>The construction of a <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><code class="xref py py-class docutils literal"><span class="pre">Cache</span></code></a> is part of the mechanics
  448. of a <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a>, and programmatic access to this
  449. cache is typically via the <code class="xref py py-attr docutils literal"><span class="pre">Template.cache</span></code> attribute.</p>
  450. <dl class="method">
  451. <dt id="mako.cache.Cache.get">
  452. <code class="descname">get</code><span class="sig-paren">(</span><em>key</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.Cache.get" title="Permalink to this definition">¶</a></dt>
  453. <dd><p>Retrieve a value from the cache.</p>
  454. <table class="docutils field-list" frame="void" rules="none">
  455. <col class="field-name" />
  456. <col class="field-body" />
  457. <tbody valign="top">
  458. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  459. <li><span class="target" id="mako.cache.Cache.get.params.key"></span><strong>key</strong><a class="paramlink headerlink reference internal" href="#mako.cache.Cache.get.params.key">¶</a> &#8211; the value&#8217;s key.</li>
  460. <li><span class="target" id="mako.cache.Cache.get.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#mako.cache.Cache.get.params.**kw">¶</a> &#8211; cache configuration arguments. The
  461. backend is configured using these arguments upon first request.
  462. Subsequent requests that use the same series of configuration
  463. values will use that same backend.</li>
  464. </ul>
  465. </td>
  466. </tr>
  467. </tbody>
  468. </table>
  469. </dd></dl>
  470. <dl class="method">
  471. <dt id="mako.cache.Cache.get_or_create">
  472. <code class="descname">get_or_create</code><span class="sig-paren">(</span><em>key</em>, <em>creation_function</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.Cache.get_or_create" title="Permalink to this definition">¶</a></dt>
  473. <dd><p>Retrieve a value from the cache, using the given creation function
  474. to generate a new value.</p>
  475. </dd></dl>
  476. <dl class="attribute">
  477. <dt id="mako.cache.Cache.id">
  478. <code class="descname">id</code><em class="property"> = None</em><a class="headerlink" href="#mako.cache.Cache.id" title="Permalink to this definition">¶</a></dt>
  479. <dd><p>Return the &#8216;id&#8217; that identifies this cache.</p>
  480. <p>This is a value that should be globally unique to the
  481. <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> associated with this cache, and can
  482. be used by a caching system to name a local container
  483. for data specific to this template.</p>
  484. </dd></dl>
  485. <dl class="attribute">
  486. <dt id="mako.cache.Cache.impl">
  487. <code class="descname">impl</code><em class="property"> = None</em><a class="headerlink" href="#mako.cache.Cache.impl" title="Permalink to this definition">¶</a></dt>
  488. <dd><p>Provide the <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></code></a> in use by this <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><code class="xref py py-class docutils literal"><span class="pre">Cache</span></code></a>.</p>
  489. <p>This accessor allows a <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></code></a> with additional
  490. methods beyond that of <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><code class="xref py py-class docutils literal"><span class="pre">Cache</span></code></a> to be used programmatically.</p>
  491. </dd></dl>
  492. <dl class="method">
  493. <dt id="mako.cache.Cache.invalidate">
  494. <code class="descname">invalidate</code><span class="sig-paren">(</span><em>key</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.Cache.invalidate" title="Permalink to this definition">¶</a></dt>
  495. <dd><p>Invalidate a value in the cache.</p>
  496. <table class="docutils field-list" frame="void" rules="none">
  497. <col class="field-name" />
  498. <col class="field-body" />
  499. <tbody valign="top">
  500. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  501. <li><span class="target" id="mako.cache.Cache.invalidate.params.key"></span><strong>key</strong><a class="paramlink headerlink reference internal" href="#mako.cache.Cache.invalidate.params.key">¶</a> &#8211; the value&#8217;s key.</li>
  502. <li><span class="target" id="mako.cache.Cache.invalidate.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#mako.cache.Cache.invalidate.params.**kw">¶</a> &#8211; cache configuration arguments. The
  503. backend is configured using these arguments upon first request.
  504. Subsequent requests that use the same series of configuration
  505. values will use that same backend.</li>
  506. </ul>
  507. </td>
  508. </tr>
  509. </tbody>
  510. </table>
  511. </dd></dl>
  512. <dl class="method">
  513. <dt id="mako.cache.Cache.invalidate_body">
  514. <code class="descname">invalidate_body</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.Cache.invalidate_body" title="Permalink to this definition">¶</a></dt>
  515. <dd><p>Invalidate the cached content of the &#8220;body&#8221; method for this
  516. template.</p>
  517. </dd></dl>
  518. <dl class="method">
  519. <dt id="mako.cache.Cache.invalidate_closure">
  520. <code class="descname">invalidate_closure</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.Cache.invalidate_closure" title="Permalink to this definition">¶</a></dt>
  521. <dd><p>Invalidate a nested <code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code> within this template.</p>
  522. <p>Caching of nested defs is a blunt tool as there is no
  523. management of scope &#8211; nested defs that use cache tags
  524. need to have names unique of all other nested defs in the
  525. template, else their content will be overwritten by
  526. each other.</p>
  527. </dd></dl>
  528. <dl class="method">
  529. <dt id="mako.cache.Cache.invalidate_def">
  530. <code class="descname">invalidate_def</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.Cache.invalidate_def" title="Permalink to this definition">¶</a></dt>
  531. <dd><p>Invalidate the cached content of a particular <code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code> within this
  532. template.</p>
  533. </dd></dl>
  534. <dl class="method">
  535. <dt id="mako.cache.Cache.put">
  536. <code class="descname">put</code><span class="sig-paren">(</span><em>key</em>, <em>value</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.Cache.put" title="Permalink to this definition">¶</a></dt>
  537. <dd><p>A synonym for <a class="reference internal" href="#mako.cache.Cache.set" title="mako.cache.Cache.set"><code class="xref py py-meth docutils literal"><span class="pre">Cache.set()</span></code></a>.</p>
  538. <p>This is here for backwards compatibility.</p>
  539. </dd></dl>
  540. <dl class="method">
  541. <dt id="mako.cache.Cache.set">
  542. <code class="descname">set</code><span class="sig-paren">(</span><em>key</em>, <em>value</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.Cache.set" title="Permalink to this definition">¶</a></dt>
  543. <dd><p>Place a value in the cache.</p>
  544. <table class="docutils field-list" frame="void" rules="none">
  545. <col class="field-name" />
  546. <col class="field-body" />
  547. <tbody valign="top">
  548. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  549. <li><span class="target" id="mako.cache.Cache.set.params.key"></span><strong>key</strong><a class="paramlink headerlink reference internal" href="#mako.cache.Cache.set.params.key">¶</a> &#8211; the value&#8217;s key.</li>
  550. <li><span class="target" id="mako.cache.Cache.set.params.value"></span><strong>value</strong><a class="paramlink headerlink reference internal" href="#mako.cache.Cache.set.params.value">¶</a> &#8211; the value.</li>
  551. <li><span class="target" id="mako.cache.Cache.set.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#mako.cache.Cache.set.params.**kw">¶</a> &#8211; cache configuration arguments.</li>
  552. </ul>
  553. </td>
  554. </tr>
  555. </tbody>
  556. </table>
  557. </dd></dl>
  558. <dl class="attribute">
  559. <dt id="mako.cache.Cache.starttime">
  560. <code class="descname">starttime</code><em class="property"> = None</em><a class="headerlink" href="#mako.cache.Cache.starttime" title="Permalink to this definition">¶</a></dt>
  561. <dd><p>Epochal time value for when the owning <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> was
  562. first compiled.</p>
  563. <p>A cache implementation may wish to invalidate data earlier than
  564. this timestamp; this has the effect of the cache for a specific
  565. <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> starting clean any time the <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a>
  566. is recompiled, such as when the original template file changed on
  567. the filesystem.</p>
  568. </dd></dl>
  569. </dd></dl>
  570. <dl class="class">
  571. <dt id="mako.cache.CacheImpl">
  572. <em class="property">class </em><code class="descclassname">mako.cache.</code><code class="descname">CacheImpl</code><span class="sig-paren">(</span><em>cache</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.CacheImpl" title="Permalink to this definition">¶</a></dt>
  573. <dd><p>Bases: <code class="xref py py-class docutils literal"><span class="pre">object</span></code></p>
  574. <p>Provide a cache implementation for use by <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><code class="xref py py-class docutils literal"><span class="pre">Cache</span></code></a>.</p>
  575. <dl class="method">
  576. <dt id="mako.cache.CacheImpl.get">
  577. <code class="descname">get</code><span class="sig-paren">(</span><em>key</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.CacheImpl.get" title="Permalink to this definition">¶</a></dt>
  578. <dd><p>Retrieve a value from the cache.</p>
  579. <table class="docutils field-list" frame="void" rules="none">
  580. <col class="field-name" />
  581. <col class="field-body" />
  582. <tbody valign="top">
  583. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  584. <li><span class="target" id="mako.cache.CacheImpl.get.params.key"></span><strong>key</strong><a class="paramlink headerlink reference internal" href="#mako.cache.CacheImpl.get.params.key">¶</a> &#8211; the value&#8217;s key.</li>
  585. <li><span class="target" id="mako.cache.CacheImpl.get.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#mako.cache.CacheImpl.get.params.**kw">¶</a> &#8211; cache configuration arguments.</li>
  586. </ul>
  587. </td>
  588. </tr>
  589. </tbody>
  590. </table>
  591. </dd></dl>
  592. <dl class="method">
  593. <dt id="mako.cache.CacheImpl.get_or_create">
  594. <code class="descname">get_or_create</code><span class="sig-paren">(</span><em>key</em>, <em>creation_function</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.CacheImpl.get_or_create" title="Permalink to this definition">¶</a></dt>
  595. <dd><p>Retrieve a value from the cache, using the given creation function
  596. to generate a new value.</p>
  597. <p>This function <em>must</em> return a value, either from
  598. the cache, or via the given creation function.
  599. If the creation function is called, the newly
  600. created value should be populated into the cache
  601. under the given key before being returned.</p>
  602. <table class="docutils field-list" frame="void" rules="none">
  603. <col class="field-name" />
  604. <col class="field-body" />
  605. <tbody valign="top">
  606. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  607. <li><span class="target" id="mako.cache.CacheImpl.get_or_create.params.key"></span><strong>key</strong><a class="paramlink headerlink reference internal" href="#mako.cache.CacheImpl.get_or_create.params.key">¶</a> &#8211; the value&#8217;s key.</li>
  608. <li><span class="target" id="mako.cache.CacheImpl.get_or_create.params.creation_function"></span><strong>creation_function</strong><a class="paramlink headerlink reference internal" href="#mako.cache.CacheImpl.get_or_create.params.creation_function">¶</a> &#8211; function that when called generates
  609. a new value.</li>
  610. <li><span class="target" id="mako.cache.CacheImpl.get_or_create.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#mako.cache.CacheImpl.get_or_create.params.**kw">¶</a> &#8211; cache configuration arguments.</li>
  611. </ul>
  612. </td>
  613. </tr>
  614. </tbody>
  615. </table>
  616. </dd></dl>
  617. <dl class="method">
  618. <dt id="mako.cache.CacheImpl.invalidate">
  619. <code class="descname">invalidate</code><span class="sig-paren">(</span><em>key</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.CacheImpl.invalidate" title="Permalink to this definition">¶</a></dt>
  620. <dd><p>Invalidate a value in the cache.</p>
  621. <table class="docutils field-list" frame="void" rules="none">
  622. <col class="field-name" />
  623. <col class="field-body" />
  624. <tbody valign="top">
  625. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  626. <li><span class="target" id="mako.cache.CacheImpl.invalidate.params.key"></span><strong>key</strong><a class="paramlink headerlink reference internal" href="#mako.cache.CacheImpl.invalidate.params.key">¶</a> &#8211; the value&#8217;s key.</li>
  627. <li><span class="target" id="mako.cache.CacheImpl.invalidate.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#mako.cache.CacheImpl.invalidate.params.**kw">¶</a> &#8211; cache configuration arguments.</li>
  628. </ul>
  629. </td>
  630. </tr>
  631. </tbody>
  632. </table>
  633. </dd></dl>
  634. <dl class="attribute">
  635. <dt id="mako.cache.CacheImpl.pass_context">
  636. <code class="descname">pass_context</code><em class="property"> = False</em><a class="headerlink" href="#mako.cache.CacheImpl.pass_context" title="Permalink to this definition">¶</a></dt>
  637. <dd><p>If <code class="docutils literal"><span class="pre">True</span></code>, the <a class="reference internal" href="runtime.html#mako.runtime.Context" title="mako.runtime.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a> will be passed to
  638. <a class="reference internal" href="#mako.cache.CacheImpl.get_or_create" title="mako.cache.CacheImpl.get_or_create"><code class="xref py py-meth docutils literal"><span class="pre">get_or_create</span></code></a> as the name <code class="docutils literal"><span class="pre">'context'</span></code>.</p>
  639. </dd></dl>
  640. <dl class="method">
  641. <dt id="mako.cache.CacheImpl.set">
  642. <code class="descname">set</code><span class="sig-paren">(</span><em>key</em>, <em>value</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.CacheImpl.set" title="Permalink to this definition">¶</a></dt>
  643. <dd><p>Place a value in the cache.</p>
  644. <table class="docutils field-list" frame="void" rules="none">
  645. <col class="field-name" />
  646. <col class="field-body" />
  647. <tbody valign="top">
  648. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  649. <li><span class="target" id="mako.cache.CacheImpl.set.params.key"></span><strong>key</strong><a class="paramlink headerlink reference internal" href="#mako.cache.CacheImpl.set.params.key">¶</a> &#8211; the value&#8217;s key.</li>
  650. <li><span class="target" id="mako.cache.CacheImpl.set.params.value"></span><strong>value</strong><a class="paramlink headerlink reference internal" href="#mako.cache.CacheImpl.set.params.value">¶</a> &#8211; the value.</li>
  651. <li><span class="target" id="mako.cache.CacheImpl.set.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#mako.cache.CacheImpl.set.params.**kw">¶</a> &#8211; cache configuration arguments.</li>
  652. </ul>
  653. </td>
  654. </tr>
  655. </tbody>
  656. </table>
  657. </dd></dl>
  658. </dd></dl>
  659. <dl class="function">
  660. <dt id="mako.cache.register_plugin">
  661. <code class="descclassname">mako.cache.</code><code class="descname">register_plugin</code><span class="sig-paren">(</span><em>self</em>, <em>name</em>, <em>modulepath</em>, <em>objname</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.cache.register_plugin" title="Permalink to this definition">¶</a></dt>
  662. <dd></dd></dl>
  663. <dl class="class">
  664. <dt id="mako.ext.beaker_cache.BeakerCacheImpl">
  665. <em class="property">class </em><code class="descclassname">mako.ext.beaker_cache.</code><code class="descname">BeakerCacheImpl</code><span class="sig-paren">(</span><em>cache</em><span class="sig-paren">)</span><a class="headerlink" href="#mako.ext.beaker_cache.BeakerCacheImpl" title="Permalink to this definition">¶</a></dt>
  666. <dd><p>Bases: <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">mako.cache.CacheImpl</span></code></a></p>
  667. <p>A <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><code class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></code></a> provided for the Beaker caching system.</p>
  668. <p>This plugin is used by default, based on the default
  669. value of <code class="docutils literal"><span class="pre">'beaker'</span></code> for the <code class="docutils literal"><span class="pre">cache_impl</span></code> parameter of the
  670. <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> or <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><code class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></code></a> classes.</p>
  671. </dd></dl>
  672. </div>
  673. </div>
  674. </div>
  675. </div>
  676. <div id="docs-bottom-navigation" class="docs-navigation-links">
  677. Previous:
  678. <a href="unicode.html" title="previous chapter">The Unicode Chapter</a>
  679. Next:
  680. <a href="changelog.html" title="next chapter">Changelog</a>
  681. <div id="docs-copyright">
  682. &copy; Copyright the Mako authors and contributors.
  683. Documentation generated using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.5.3
  684. with Mako templates.
  685. </div>
  686. </div>
  687. </div>
  688. <div class="clearfix">
  689. <hr/>
  690. <div class="copyright">Website content copyright &copy; by Michael Bayer.
  691. All rights reserved. Mako and its documentation are licensed
  692. under the MIT license. mike(&)zzzcomputing.com</div>
  693. </div>
  694. </div>
  695. </body>
  696. </html>