summaryrefslogtreecommitdiff
path: root/examples/dogpile_caching/relation_caching.py
blob: d40752e4839a9b664eb1f559d23725487d11afc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""relationship_caching.py

Load a set of Person and Address objects, specifying that
related PostalCode, City, Country objects should be pulled from long
term cache.

"""
from .environment import Session, root
from .model import Person, cache_address_bits
from sqlalchemy.orm import joinedload
import os

for p in Session.query(Person).options(joinedload(Person.addresses), cache_address_bits):
    print(p.format_full())


print("\n\nIf this was the first run of relationship_caching.py, SQL was likely emitted to "\
        "load postal codes, cities, countries.\n"\
        "If run a second time, assuming the cache is still valid, "\
        "only a single SQL statement will run - all "\
        "related data is pulled from cache.\n"\
        "To clear the cache, delete the file %r.  \n"\
        "This will cause a re-load of cities, postal codes and countries on "\
        "the next run.\n"\
        % os.path.join(root, 'cache.dbm'))