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

« back to all changes in this revision

Viewing changes to examples/beaker_caching/__init__.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
 
"""
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
5
 
as well.
6
 
 
7
 
In this demo, the following techniques are illustrated:
8
 
 
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.
17
 
 
18
 
E.g.::
19
 
 
20
 
    # query for Person objects, specifying cache
21
 
    q = Session.query(Person).options(FromCache("default", "all_people"))
22
 
 
23
 
    # specify that each Person's "addresses" collection comes from
24
 
    # cache too
25
 
    q = q.options(RelationshipCache("default", "by_person", Person.addresses))
26
 
 
27
 
    # query
28
 
    print q.all()
29
 
 
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
36
 
pull from cache.
37
 
 
38
 
The demo scripts themselves, in order of complexity, are run as follows::
39
 
 
40
 
   python examples/beaker_caching/helloworld.py
41
 
 
42
 
   python examples/beaker_caching/relationship_caching.py
43
 
 
44
 
   python examples/beaker_caching/advanced.py
45
 
 
46
 
   python examples/beaker_caching/local_session_caching.py
47
 
 
48
 
 
49
 
Listing of files:
50
 
 
51
 
    environment.py - Establish the Session, the Beaker cache
52
 
    manager, data / cache file paths, and configurations,
53
 
    bootstrap fixture data if necessary.
54
 
 
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.
58
 
 
59
 
    model.py - The datamodel, which represents Person that has multiple
60
 
    Address objects, each with PostalCode, City, Country
61
 
 
62
 
    fixture_data.py - creates demo PostalCode, Address, Person objects
63
 
    in the database.
64
 
 
65
 
    helloworld.py - the basic idea.
66
 
 
67
 
    relationship_caching.py - Illustrates how to add cache options on
68
 
    relationship endpoints, so that lazyloads load from cache.
69
 
 
70
 
    advanced.py - Further examples of how to use FromCache.  Combines
71
 
    techniques from the first two scripts.
72
 
 
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.
77
 
 
78
 
"""