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

« back to all changes in this revision

Viewing changes to examples/beaker_caching/helloworld.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
 
"""helloworld.py
2
 
 
3
 
Illustrate how to load some data, and cache the results.
4
 
 
5
 
"""
6
 
 
7
 
from environment import Session
8
 
from model import Person
9
 
from caching_query import FromCache
10
 
 
11
 
# load Person objects.  cache the result under the namespace "all_people".
12
 
print "loading people...."
13
 
people = Session.query(Person).options(FromCache("default", "all_people")).all()
14
 
 
15
 
# remove the Session.  next query starts from scratch.
16
 
Session.remove()
17
 
 
18
 
# load again, using the same FromCache option. now they're cached
19
 
# under "all_people", no SQL is emitted.
20
 
print "loading people....again!"
21
 
people = Session.query(Person).options(FromCache("default", "all_people")).all()
22
 
 
23
 
# want to load on some different kind of query ?  change the namespace
24
 
# you send to FromCache
25
 
print "loading people two through twelve"
26
 
people_two_through_twelve = Session.query(Person).\
27
 
                            options(FromCache("default", "people_on_range")).\
28
 
                            filter(Person.name.between("person 02", "person 12")).\
29
 
                            all()
30
 
 
31
 
# the data is cached under the "namespace" you send to FromCache, *plus*
32
 
# the bind parameters of the query.    So this query, having
33
 
# different literal parameters under "Person.name.between()" than the
34
 
# previous one, issues new SQL...
35
 
print "loading people five through fifteen"
36
 
people_five_through_fifteen = Session.query(Person).\
37
 
                            options(FromCache("default", "people_on_range")).\
38
 
                            filter(Person.name.between("person 05", "person 15")).\
39
 
                            all()
40
 
 
41
 
 
42
 
# ... but using the same params as are already cached, no SQL
43
 
print "loading people two through twelve...again!"
44
 
people_two_through_twelve = Session.query(Person).\
45
 
                            options(FromCache("default", "people_on_range")).\
46
 
                            filter(Person.name.between("person 02", "person 12")).\
47
 
                            all()
48
 
 
49
 
 
50
 
# invalidate the cache for the three queries we've done.  Recreate
51
 
# each Query, which includes at the very least the same FromCache,
52
 
# same list of objects to be loaded, and the same parameters in the
53
 
# same order, then call invalidate().
54
 
print "invalidating everything"
55
 
Session.query(Person).options(FromCache("default", "all_people")).invalidate()
56
 
Session.query(Person).\
57
 
            options(FromCache("default", "people_on_range")).\
58
 
            filter(Person.name.between("person 02", "person 12")).invalidate()
59
 
Session.query(Person).\
60
 
            options(FromCache("default", "people_on_range")).\
61
 
            filter(Person.name.between("person 05", "person 15")).invalidate()
62