<!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> examples.dogpile_caching.relationship_caching — SQLAlchemy 1.2 Documentation </title> <!-- begin iterate through site-imported + sphinx environment css_files --> <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" /> <link rel="stylesheet" href="../../../_static/docs.css" type="text/css" /> <link rel="stylesheet" href="../../../_static/changelog.css" type="text/css" /> <link rel="stylesheet" href="../../../_static/sphinx_paramlinks.css" type="text/css" /> <!-- end iterate through site-imported + sphinx environment css_files --> <!-- begin layout.mako headers --> <link rel="index" title="Index" href="../../../genindex.html" /> <link rel="search" title="Search" href="../../../search.html" /> <link rel="copyright" title="Copyright" href="../../../copyright.html" /> <link rel="top" title="SQLAlchemy 1.2 Documentation" href="../../../index.html" /> <link rel="up" title="Module code" href="../../index.html" /> <!-- end layout.mako headers --> </head> <body> <div id="docs-container"> <div id="docs-top-navigation-container" class="body-background"> <div id="docs-header"> <div id="docs-version-header"> Release: <span class="version-num">1.2.19</span> | Release Date: April 15, 2019 </div> <h1>SQLAlchemy 1.2 Documentation</h1> </div> </div> <div id="docs-body-container"> <div id="fixed-sidebar" class=""> <div id="index-nav"> <form class="search" action="../../../search.html" method="get"> <label> Search terms: <input type="text" placeholder="search..." name="q" size="12" /> </label> <input type="submit" value="Search" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p> <a href="../../../contents.html">Contents</a> | <a href="../../../genindex.html">Index</a> </p> </div> </div> <div id="docs-body" class="" > <h1>Source code for examples.dogpile_caching.relationship_caching</h1><div class="highlight"><pre> <span></span><span class="sd">"""Illustrates how to add cache options on</span> <span class="sd">relationship endpoints, so that lazyloads load from cache.</span> <span class="sd">Load a set of Person and Address objects, specifying that</span> <span class="sd">related PostalCode, City, Country objects should be pulled from long</span> <span class="sd">term cache.</span> <span class="sd">"""</span> <span class="kn">import</span> <span class="nn">os</span> <span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="k">import</span> <span class="n">joinedload</span> <span class="kn">from</span> <span class="nn">.environment</span> <span class="k">import</span> <span class="n">root</span> <span class="kn">from</span> <span class="nn">.environment</span> <span class="k">import</span> <span class="n">Session</span> <span class="kn">from</span> <span class="nn">.model</span> <span class="k">import</span> <span class="n">cache_address_bits</span> <span class="kn">from</span> <span class="nn">.model</span> <span class="k">import</span> <span class="n">Person</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">Session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Person</span><span class="p">)</span><span class="o">.</span><span class="n">options</span><span class="p">(</span> <span class="n">joinedload</span><span class="p">(</span><span class="n">Person</span><span class="o">.</span><span class="n">addresses</span><span class="p">),</span> <span class="n">cache_address_bits</span> <span class="p">):</span> <span class="nb">print</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">format_full</span><span class="p">())</span> <span class="nb">print</span><span class="p">(</span> <span class="s2">"</span><span class="se">\n\n</span><span class="s2">If this was the first run of relationship_caching.py, "</span> <span class="s2">"SQL was likely emitted to "</span> <span class="s2">"load postal codes, cities, countries.</span><span class="se">\n</span><span class="s2">"</span> <span class="s2">"If run a second time, assuming the cache is still valid, "</span> <span class="s2">"only a single SQL statement will run - all "</span> <span class="s2">"related data is pulled from cache.</span><span class="se">\n</span><span class="s2">"</span> <span class="s2">"To clear the cache, delete the file </span><span class="si">%r</span><span class="s2">. </span><span class="se">\n</span><span class="s2">"</span> <span class="s2">"This will cause a re-load of cities, postal codes and countries on "</span> <span class="s2">"the next run.</span><span class="se">\n</span><span class="s2">"</span> <span class="o">%</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s2">"cache.dbm"</span><span class="p">)</span> <span class="p">)</span></pre></div> </div> </div> <div id="docs-bottom-navigation" class="docs-navigation-links"> <div id="docs-copyright"> © <a href="../../../copyright.html">Copyright</a> 2007-2019, the SQLAlchemy authors and contributors. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1. </div> </div> </div> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../../', VERSION: '1.2.19', COLLAPSE_MODINDEX: false, FILE_SUFFIX: '.html' }; </script> <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script> <!-- begin iterate through sphinx environment script_files --> <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> <script type="text/javascript" src="../../../_static/language_data.js"></script> <!-- end iterate through sphinx environment script_files --> <script type="text/javascript" src="../../../_static/detectmobile.js"></script> <script type="text/javascript" src="../../../_static/init.js"></script> </body> </html>