2
Illustrates how to embed Beaker cache functionality within
3
the Query object, allowing full cache control as well as the
4
ability to pull "lazy loaded" attributes from long term cache
7
In this demo, the following techniques are illustrated:
9
* Using custom subclasses of Query
10
* Basic technique of circumventing Query to pull from a
11
custom cache source instead of the database.
12
* Rudimental caching with Beaker, using "regions" which allow
13
global control over a fixed set of configurations.
14
* Using custom MapperOption objects to configure options on
15
a Query, including the ability to invoke the options
16
deep within an object graph when lazy loads occur.
20
# query for Person objects, specifying cache
21
q = Session.query(Person).options(FromCache("default", "all_people"))
23
# specify that each Person's "addresses" collection comes from
25
q = q.options(RelationshipCache("default", "by_person", Person.addresses))
30
To run, both SQLAlchemy and Beaker (1.4 or greater) must be
31
installed or on the current PYTHONPATH. The demo will create a local
32
directory for datafiles, insert initial data, and run. Running the
33
demo a second time will utilize the cache files already present, and
34
exactly one SQL statement against two tables will be emitted - the
35
displayed result however will utilize dozens of lazyloads that all
38
The demo scripts themselves, in order of complexity, are run as follows::
40
python examples/beaker_caching/helloworld.py
42
python examples/beaker_caching/relationship_caching.py
44
python examples/beaker_caching/advanced.py
46
python examples/beaker_caching/local_session_caching.py
51
environment.py - Establish the Session, the Beaker cache
52
manager, data / cache file paths, and configurations,
53
bootstrap fixture data if necessary.
55
caching_query.py - Represent functions and classes
56
which allow the usage of Beaker caching with SQLAlchemy.
57
Introduces a query option called FromCache.
59
model.py - The datamodel, which represents Person that has multiple
60
Address objects, each with PostalCode, City, Country
62
fixture_data.py - creates demo PostalCode, Address, Person objects
65
helloworld.py - the basic idea.
67
relationship_caching.py - Illustrates how to add cache options on
68
relationship endpoints, so that lazyloads load from cache.
70
advanced.py - Further examples of how to use FromCache. Combines
71
techniques from the first two scripts.
73
local_session_caching.py - Grok everything so far ? This example
74
creates a new Beaker container that will persist data in a dictionary
75
which is local to the current session. remove() the session
76
and the cache is gone.