helloworld.py :  » Database » SQLAlchemy » SQLAlchemy-0.6.0 » examples » beaker_caching » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » SQLAlchemy 
SQLAlchemy » SQLAlchemy 0.6.0 » examples » beaker_caching » helloworld.py
"""helloworld.py

Illustrate how to load some data, and cache the results.

"""

import environment
from model import Person
from meta import Session,FromCache

# load Person objects.  cache the result under the namespace "all_people".
print "loading people...."
people = Session.query(Person).options(FromCache("default", "all_people")).all()

# remove the Session.  next query starts from scratch.
Session.remove()

# load again, using the same FromCache option. now they're cached 
# under "all_people", no SQL is emitted.
print "loading people....again!"
people = Session.query(Person).options(FromCache("default", "all_people")).all()

# want to load on some different kind of query ?  change the namespace 
# you send to FromCache
print "loading people two through twelve"
people_two_through_twelve = Session.query(Person).\
                            options(FromCache("default", "people_on_range")).\
                            filter(Person.name.between("person 02", "person 12")).\
                            all()

# the data is cached under the "namespace" you send to FromCache, *plus*
# the bind parameters of the query.    So this query, having
# different literal parameters under "Person.name.between()" than the 
# previous one, issues new SQL...
print "loading people five through fifteen"
people_five_through_fifteen = Session.query(Person).\
                            options(FromCache("default", "people_on_range")).\
                            filter(Person.name.between("person 05", "person 15")).\
                            all()


# ... but using the same params as are already cached, no SQL
print "loading people two through twelve...again!"
people_two_through_twelve = Session.query(Person).\
                            options(FromCache("default", "people_on_range")).\
                            filter(Person.name.between("person 02", "person 12")).\
                            all()


# invalidate the cache for the three queries we've done.  Recreate
# each Query, which includes at the very least the same FromCache, 
# same list of objects to be loaded, and the same parameters in the 
# same order, then call invalidate().
print "invalidating everything"
Session.query(Person).options(FromCache("default", "all_people")).invalidate()
Session.query(Person).\
            options(FromCache("default", "people_on_range")).\
            filter(Person.name.between("person 02", "person 12")).invalidate()
Session.query(Person).\
            options(FromCache("default", "people_on_range")).\
            filter(Person.name.between("person 05", "person 15")).invalidate()

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.