displayed result however will utilize dozens of lazyloads that all
pull from cache.
-Two endpoint scripts, "demo.py" and "ad_hoc.py", are run as follows:
+Three endpoint scripts, in order of complexity, are run as follows:
- python examples/beaker_caching/demo.py
+ python examples/beaker_caching/helloworld.py
- python examples/beaker_caching/ad_hoc.py
+ python examples/beaker_caching/relation_caching.py
+
+ python examples/beaker_caching/advanced.py
Listing of files:
fixture_data.py - creates demo PostalCode, Address, Person objects
in the database.
-demo.py - The first script to run - illustrates loading a list of
-Person / Address objects. When run a second time, most data is
-cached and only one SQL statement is emitted.
+helloworld.py - the basic idea.
-ad_hoc.py - Further examples of how to use FromCache. Illustrates
-front-end usage, cache invalidation, loading related collections
-from cache vs. eager loading of collections.
+relation_caching.py - Illustrates how to add cache options on
+relation endpoints, so that lazyloads load from cache.
+advanced.py - Further examples of how to use FromCache. Combines
+techniques from the first two scripts.
--- /dev/null
+"""helloworld.py
+
+Illustrate how to load some data, and cache the results.
+
+"""
+
+import __init__ # if running as a script
+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()
+
-"""demo.py
+"""relation_caching.py
Load a set of Person and Address objects, specifying that
-PostalCode, City, Country objects should be pulled from long
+related PostalCode, City, Country objects should be pulled from long
term cache.
"""
print p.format_full()
-print "\n\nIf this was the first run of demo.py, SQL was likely emitted to "\
+print "\n\nIf this was the first run of relation_caching.py, SQL was likely emitted to "\
"load postal codes, cities, countries.\n"\
"If run a second time, only a single SQL statement will run - all "\
"related data is pulled from cache.\n"\