diff options
Diffstat (limited to 'app/static/doc/flask-docs/patterns/caching.html')
-rw-r--r-- | app/static/doc/flask-docs/patterns/caching.html | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/app/static/doc/flask-docs/patterns/caching.html b/app/static/doc/flask-docs/patterns/caching.html new file mode 100644 index 0000000..ed0d3c1 --- /dev/null +++ b/app/static/doc/flask-docs/patterns/caching.html @@ -0,0 +1,178 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Caching — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Patterns for Flask" href="index.html" /> + <link rel="next" title="View Decorators" href="viewdecorators.html" /> + <link rel="prev" title="Uploading Files" href="fileuploads.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="viewdecorators.html" title="View Decorators" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="fileuploads.html" title="Uploading Files" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Patterns for Flask</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="caching"> +<span id="caching-pattern"></span><h1>Caching<a class="headerlink" href="#caching" title="Permalink to this headline">¶</a></h1> +<p>When your application runs slow, throw some caches in. Well, at least +it’s the easiest way to speed up things. What does a cache do? Say you +have a function that takes some time to complete but the results would +still be good enough if they were 5 minutes old. So then the idea is that +you actually put the result of that calculation into a cache for some +time.</p> +<p>Flask itself does not provide caching for you, but Werkzeug, one of the +libraries it is based on, has some very basic cache support. It supports +multiple cache backends, normally you want to use a memcached server.</p> +<div class="section" id="setting-up-a-cache"> +<h2>Setting up a Cache<a class="headerlink" href="#setting-up-a-cache" title="Permalink to this headline">¶</a></h2> +<p>You create a cache object once and keep it around, similar to how +<a class="reference internal" href="../api.html#flask.Flask" title="flask.Flask"><tt class="xref py py-class docutils literal"><span class="pre">Flask</span></tt></a> objects are created. If you are using the +development server you can create a +<a class="reference external" href="http://werkzeug.pocoo.org/docs/contrib/cache/#werkzeug.contrib.cache.SimpleCache" title="(in Werkzeug v0.7)"><tt class="xref py py-class docutils literal"><span class="pre">SimpleCache</span></tt></a> object, that one is a simple +cache that keeps the item stored in the memory of the Python interpreter:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.contrib.cache</span> <span class="kn">import</span> <span class="n">SimpleCache</span> +<span class="n">cache</span> <span class="o">=</span> <span class="n">SimpleCache</span><span class="p">()</span> +</pre></div> +</div> +<p>If you want to use memcached, make sure to have one of the memcache modules +supported (you get them from <a class="reference external" href="http://pypi.python.org/">PyPI</a>) and a +memcached server running somewhere. This is how you connect to such an +memcached server then:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.contrib.cache</span> <span class="kn">import</span> <span class="n">MemcachedCache</span> +<span class="n">cache</span> <span class="o">=</span> <span class="n">MemcachedCache</span><span class="p">([</span><span class="s">'127.0.0.1:11211'</span><span class="p">])</span> +</pre></div> +</div> +<p>If you are using App Engine, you can connect to the App Engine memcache +server easily:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.contrib.cache</span> <span class="kn">import</span> <span class="n">GAEMemcachedCache</span> +<span class="n">cache</span> <span class="o">=</span> <span class="n">GAEMemcachedCache</span><span class="p">()</span> +</pre></div> +</div> +</div> +<div class="section" id="using-a-cache"> +<h2>Using a Cache<a class="headerlink" href="#using-a-cache" title="Permalink to this headline">¶</a></h2> +<p>Now how can one use such a cache? There are two very important +operations: <a class="reference external" href="http://werkzeug.pocoo.org/docs/contrib/cache/#werkzeug.contrib.cache.BaseCache.get" title="(in Werkzeug v0.7)"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a> and +<a class="reference external" href="http://werkzeug.pocoo.org/docs/contrib/cache/#werkzeug.contrib.cache.BaseCache.set" title="(in Werkzeug v0.7)"><tt class="xref py py-meth docutils literal"><span class="pre">set()</span></tt></a>. This is how to use them:</p> +<p>To get an item from the cache call +<a class="reference external" href="http://werkzeug.pocoo.org/docs/contrib/cache/#werkzeug.contrib.cache.BaseCache.get" title="(in Werkzeug v0.7)"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a> with a string as key name. +If something is in the cache, it is returned. Otherwise that function +will return <cite>None</cite>:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="n">rv</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="s">'my-item'</span><span class="p">)</span> +</pre></div> +</div> +<p>To add items to the cache, use the <a class="reference external" href="http://werkzeug.pocoo.org/docs/contrib/cache/#werkzeug.contrib.cache.BaseCache.set" title="(in Werkzeug v0.7)"><tt class="xref py py-meth docutils literal"><span class="pre">set()</span></tt></a> +method instead. The first argument is the key and the second the value +that should be set. Also a timeout can be provided after which the cache +will automatically remove item.</p> +<p>Here a full example how this looks like normally:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">get_my_item</span><span class="p">():</span> + <span class="n">rv</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="s">'my-item'</span><span class="p">)</span> + <span class="k">if</span> <span class="n">rv</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> + <span class="n">rv</span> <span class="o">=</span> <span class="n">calculate_value</span><span class="p">()</span> + <span class="n">cache</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s">'my-item'</span><span class="p">,</span> <span class="n">rv</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">5</span> <span class="o">*</span> <span class="mi">60</span><span class="p">)</span> + <span class="k">return</span> <span class="n">rv</span> +</pre></div> +</div> +</div> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p> + <h3><a href="../index.html">Table Of Contents</a></h3> + <ul> +<li><a class="reference internal" href="#">Caching</a><ul> +<li><a class="reference internal" href="#setting-up-a-cache">Setting up a Cache</a></li> +<li><a class="reference internal" href="#using-a-cache">Using a Cache</a></li> +</ul> +</li> +</ul> +<h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Patterns for Flask</a><ul> + <li>Previous: <a href="fileuploads.html" title="previous chapter">Uploading Files</a></li> + <li>Next: <a href="viewdecorators.html" title="next chapter">View Decorators</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/patterns/caching.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file |