"""advanced.py
Illustrate usage of Query combined with the FromCache option,
-including front-end loading, cache invalidation, namespace techniques
-and collection caching.
+including front-end loading, cache invalidation and collection caching.
"""
from .environment import Session
-from .model import Person, Address, cache_address_bits
+from .model import Person, cache_address_bits
from .caching_query import FromCache, RelationshipCache
-from sqlalchemy.orm import joinedload
def load_name_range(start, end, invalidate=False):
"""Load Person objects on a range of names.
from sqlalchemy.ext.declarative import declarative_base
from dogpile.cache.region import make_region
import os
-import md5
+from hashlib import md5
+import sys
+py2k = sys.version_info < (3, 0)
+
+if py2k:
+ input = raw_input
# dogpile cache regions. A home base for cache configurations.
regions = {}
distill them into an md5 hash.
"""
- return md5.md5(key).hexdigest()
+ return md5(key.encode('ascii')).hexdigest()
# configure the "default" cache region.
regions['default'] = make_region(
from .model import Person
from .caching_query import FromCache
-# load Person objects. cache the result under the namespace "all_people".
+# load Person objects. cache the result in the "default" cache region
print("loading people....")
people = Session.query(Person).options(FromCache("default")).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.
+# load again, using the same FromCache option. now they're cached,
+# so no SQL is emitted.
print("loading people....again!")
people = Session.query(Person).options(FromCache("default")).all()
-# want to load on some different kind of query ? change the namespace
-# you send to FromCache
+# Specifying a different query produces a different cache key, so
+# these results are independently cached.
print("loading people two through twelve")
people_two_through_twelve = Session.query(Person).\
options(FromCache("default")).\
filter(Person.name.between("person 02", "person 12")).\
all()
-# the data is cached under the "namespace" you send to FromCache, *plus*
+# the data is cached under string structure of the SQL statement, *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...