~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to examples/beaker_caching/advanced.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""advanced.py
2
 
 
3
 
Illustrate usage of Query combined with the FromCache option,
4
 
including front-end loading, cache invalidation, namespace techniques
5
 
and collection caching.
6
 
 
7
 
"""
8
 
 
9
 
from environment import Session
10
 
from model import Person, Address, cache_address_bits
11
 
from caching_query import FromCache, RelationshipCache
12
 
from sqlalchemy.orm import joinedload
13
 
 
14
 
def load_name_range(start, end, invalidate=False):
15
 
    """Load Person objects on a range of names.
16
 
 
17
 
    start/end are integers, range is then
18
 
    "person <start>" - "person <end>".
19
 
 
20
 
    The cache option we set up is called "name_range", indicating
21
 
    a range of names for the Person class.
22
 
 
23
 
    The `Person.addresses` collections are also cached.  Its basically
24
 
    another level of tuning here, as that particular cache option
25
 
    can be transparently replaced with joinedload(Person.addresses).
26
 
    The effect is that each Person and his/her Address collection
27
 
    is cached either together or separately, affecting the kind of
28
 
    SQL that emits for unloaded Person objects as well as the distribution
29
 
    of data within the cache.
30
 
    """
31
 
    q = Session.query(Person).\
32
 
            filter(Person.name.between("person %.2d" % start, "person %.2d" % end)).\
33
 
            options(cache_address_bits).\
34
 
            options(FromCache("default", "name_range"))
35
 
 
36
 
    # have the "addresses" collection cached separately
37
 
    # each lazyload of Person.addresses loads from cache.
38
 
    q = q.options(RelationshipCache("default", "by_person", Person.addresses))
39
 
 
40
 
    # alternatively, eagerly load the "addresses" collection, so that they'd
41
 
    # be cached together.   This issues a bigger SQL statement and caches
42
 
    # a single, larger value in the cache per person rather than two
43
 
    # separate ones.
44
 
    #q = q.options(joinedload(Person.addresses))
45
 
 
46
 
    # if requested, invalidate the cache on current criterion.
47
 
    if invalidate:
48
 
        q.invalidate()
49
 
 
50
 
    return q.all()
51
 
 
52
 
print "two through twelve, possibly from cache:\n"
53
 
print ", ".join([p.name for p in load_name_range(2, 12)])
54
 
 
55
 
print "\ntwenty five through forty, possibly from cache:\n"
56
 
print ", ".join([p.name for p in load_name_range(25, 40)])
57
 
 
58
 
# loading them again, no SQL is emitted
59
 
print "\ntwo through twelve, from the cache:\n"
60
 
print ", ".join([p.name for p in load_name_range(2, 12)])
61
 
 
62
 
# but with invalidate, they are
63
 
print "\ntwenty five through forty, invalidate first:\n"
64
 
print ", ".join([p.name for p in load_name_range(25, 40, True)])
65
 
 
66
 
# illustrate the address loading from either cache/already
67
 
# on the Person
68
 
print "\n\nPeople plus addresses, two through twelve, addresses possibly from cache"
69
 
for p in load_name_range(2, 12):
70
 
    print p.format_full()
71
 
 
72
 
# illustrate the address loading from either cache/already
73
 
# on the Person
74
 
print "\n\nPeople plus addresses, two through twelve, addresses from cache"
75
 
for p in load_name_range(2, 12):
76
 
    print p.format_full()
77
 
 
78
 
print "\n\nIf this was the first run of advanced.py, try "\
79
 
        "a second run.  Only one SQL statement will be emitted."