1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
<!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 0.9 Documentation
</title>
<!-- begin iterate through SQLA + 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/sphinx_paramlinks.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/changelog.css" type="text/css" />
<!-- end iterate through SQLA + sphinx environment css_files -->
<!-- begin layout.mako headers -->
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../../',
VERSION: '0.9.8',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html'
};
</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>
<!-- 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>
<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 0.9 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">0.9.8</span> | Release Date: October 13, 2014
</div>
<h1>SQLAlchemy 0.9 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">
<input type="text" name="q" size="12" /> <input type="submit" value="Search" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p>
<a href="../../../index.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 class="sd">"""relationship_caching.py</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">from</span> <span class="nn">.environment</span> <span class="kn">import</span> <span class="n">Session</span><span class="p">,</span> <span class="n">root</span>
<span class="kn">from</span> <span class="nn">.model</span> <span class="kn">import</span> <span class="n">Person</span><span class="p">,</span> <span class="n">cache_address_bits</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">joinedload</span>
<span class="kn">import</span> <span class="nn">os</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="k">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="k">print</span><span class="p">(</span><span class="s">"</span><span class="se">\n\n</span><span class="s">If this was the first run of relationship_caching.py, SQL was likely emitted to "</span>\
<span class="s">"load postal codes, cities, countries.</span><span class="se">\n</span><span class="s">"</span>\
<span class="s">"If run a second time, assuming the cache is still valid, "</span>\
<span class="s">"only a single SQL statement will run - all "</span>\
<span class="s">"related data is pulled from cache.</span><span class="se">\n</span><span class="s">"</span>\
<span class="s">"To clear the cache, delete the file </span><span class="si">%r</span><span class="s">. </span><span class="se">\n</span><span class="s">"</span>\
<span class="s">"This will cause a re-load of cities, postal codes and countries on "</span>\
<span class="s">"the next run.</span><span class="se">\n</span><span class="s">"</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="s">'cache.dbm'</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-2014, the SQLAlchemy authors and contributors.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2b1.
</div>
</div>
</div>
</body>
</html>
|