3
Illustrate usage of Query combined with the FromCache option,
4
including front-end loading, cache invalidation, namespace techniques
5
and collection caching.
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
14
def load_name_range(start, end, invalidate=False):
15
"""Load Person objects on a range of names.
17
start/end are integers, range is then
18
"person <start>" - "person <end>".
20
The cache option we set up is called "name_range", indicating
21
a range of names for the Person class.
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.
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"))
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))
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
44
#q = q.options(joinedload(Person.addresses))
46
# if requested, invalidate the cache on current criterion.
52
print "two through twelve, possibly from cache:\n"
53
print ", ".join([p.name for p in load_name_range(2, 12)])
55
print "\ntwenty five through forty, possibly from cache:\n"
56
print ", ".join([p.name for p in load_name_range(25, 40)])
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)])
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)])
66
# illustrate the address loading from either cache/already
68
print "\n\nPeople plus addresses, two through twelve, addresses possibly from cache"
69
for p in load_name_range(2, 12):
72
# illustrate the address loading from either cache/already
74
print "\n\nPeople plus addresses, two through twelve, addresses from cache"
75
for p in load_name_range(2, 12):
78
print "\n\nIf this was the first run of advanced.py, try "\
79
"a second run. Only one SQL statement will be emitted."