File: local_session_caching.html

package info (click to toggle)
sqlalchemy 0.9.8%2Bdfsg-0.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,952 kB
  • ctags: 24,534
  • sloc: python: 152,282; ansic: 1,346; makefile: 257; xml: 17
file content (240 lines) | stat: -rw-r--r-- 13,510 bytes parent folder | download
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!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.local_session_caching
             &mdash;
    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.local_session_caching</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;local_session_caching.py</span>

<span class="sd">Grok everything so far ?   This example</span>
<span class="sd">creates a new dogpile.cache backend that will persist data in a dictionary</span>
<span class="sd">which is local to the current session.   remove() the session</span>
<span class="sd">and the cache is gone.</span>

<span class="sd">Create a new Dogpile cache backend that will store</span>
<span class="sd">cached data local to the current Session.</span>

<span class="sd">This is an advanced example which assumes familiarity</span>
<span class="sd">with the basic operation of CachingQuery.</span>

<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">dogpile.cache.api</span> <span class="kn">import</span> <span class="n">CacheBackend</span><span class="p">,</span> <span class="n">NO_VALUE</span>
<span class="kn">from</span> <span class="nn">dogpile.cache.region</span> <span class="kn">import</span> <span class="n">register_backend</span>

<span class="k">class</span> <span class="nc">ScopedSessionBackend</span><span class="p">(</span><span class="n">CacheBackend</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A dogpile backend which will cache objects locally on</span>
<span class="sd">    the current session.</span>

<span class="sd">    When used with the query_cache system, the effect is that the objects</span>
<span class="sd">    in the cache are the same as that within the session - the merge()</span>
<span class="sd">    is a formality that doesn&#39;t actually create a second instance.</span>
<span class="sd">    This makes it safe to use for updates of data from an identity</span>
<span class="sd">    perspective (still not ideal for deletes though).</span>

<span class="sd">    When the session is removed, the cache is gone too, so the cache</span>
<span class="sd">    is automatically disposed upon session.remove().</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arguments</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span> <span class="o">=</span> <span class="n">arguments</span><span class="p">[</span><span class="s">&#39;scoped_session&#39;</span><span class="p">]</span>

    <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="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cache_dictionary</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">NO_VALUE</span><span class="p">)</span>

    <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="bp">self</span><span class="o">.</span><span class="n">_cache_dictionary</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="k">def</span> <span class="nf">delete</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="bp">self</span><span class="o">.</span><span class="n">_cache_dictionary</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>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">_cache_dictionary</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the cache dictionary linked to the current Session.&quot;&quot;&quot;</span>

        <span class="n">sess</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoped_session</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">cache_dict</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">_cache_dictionary</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="n">sess</span><span class="o">.</span><span class="n">_cache_dictionary</span> <span class="o">=</span> <span class="n">cache_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">return</span> <span class="n">cache_dict</span>

<span class="n">register_backend</span><span class="p">(</span><span class="s">&quot;sqlalchemy.session&quot;</span><span class="p">,</span> <span class="n">__name__</span><span class="p">,</span> <span class="s">&quot;ScopedSessionBackend&quot;</span><span class="p">)</span>


<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</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">regions</span>
    <span class="kn">from</span> <span class="nn">.caching_query</span> <span class="kn">import</span> <span class="n">FromCache</span>
    <span class="kn">from</span> <span class="nn">dogpile.cache</span> <span class="kn">import</span> <span class="n">make_region</span>

    <span class="c"># set up a region based on the ScopedSessionBackend,</span>
    <span class="c"># pointing to the scoped_session declared in the example</span>
    <span class="c"># environment.</span>
    <span class="n">regions</span><span class="p">[</span><span class="s">&#39;local_session&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">make_region</span><span class="p">()</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span>
        <span class="s">&#39;sqlalchemy.session&#39;</span><span class="p">,</span>
        <span class="n">arguments</span><span class="o">=</span><span class="p">{</span>
            <span class="s">&quot;scoped_session&quot;</span><span class="p">:</span> <span class="n">Session</span>
        <span class="p">}</span>
    <span class="p">)</span>

    <span class="kn">from</span> <span class="nn">.model</span> <span class="kn">import</span> <span class="n">Person</span>

    <span class="c"># query to load Person by name, with criterion</span>
    <span class="c"># of &quot;person 10&quot;</span>
    <span class="n">q</span> <span class="o">=</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">FromCache</span><span class="p">(</span><span class="s">&quot;local_session&quot;</span><span class="p">))</span><span class="o">.</span>\
                    <span class="nb">filter</span><span class="p">(</span><span class="n">Person</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s">&quot;person 10&quot;</span><span class="p">)</span>

    <span class="c"># load from DB</span>
    <span class="n">person10</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">one</span><span class="p">()</span>

    <span class="c"># next call, the query is cached.</span>
    <span class="n">person10</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">one</span><span class="p">()</span>

    <span class="c"># clear out the Session.  The &quot;_cache_dictionary&quot; dictionary</span>
    <span class="c"># disappears with it.</span>
    <span class="n">Session</span><span class="o">.</span><span class="n">remove</span><span class="p">()</span>

    <span class="c"># query calls from DB again</span>
    <span class="n">person10</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">one</span><span class="p">()</span>

    <span class="c"># identity is preserved - person10 is the *same* object that&#39;s</span>
    <span class="c"># ultimately inside the cache.   So it is safe to manipulate</span>
    <span class="c"># the not-queried-for attributes of objects when using such a</span>
    <span class="c"># cache without the need to invalidate - however, any change</span>
    <span class="c"># that would change the results of a cached query, such as</span>
    <span class="c"># inserts, deletes, or modification to attributes that are</span>
    <span class="c"># part of query criterion, still require careful invalidation.</span>
    <span class="n">cache</span><span class="p">,</span> <span class="n">key</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">_get_cache_plus_key</span><span class="p">()</span>
    <span class="k">assert</span> <span class="n">person10</span> <span class="ow">is</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><span class="mi">0</span><span class="p">]</span></pre></div>
    </div>

</div>

<div id="docs-bottom-navigation" class="docs-navigation-links">

    <div id="docs-copyright">
        &copy; <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>