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

« back to all changes in this revision

Viewing changes to examples/dogpile_caching/environment.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
"""environment.py
 
2
 
 
3
Establish data / cache file paths, and configurations,
 
4
bootstrap fixture data if necessary.
 
5
 
 
6
"""
 
7
import caching_query
 
8
from sqlalchemy import create_engine
 
9
from sqlalchemy.orm import scoped_session, sessionmaker
 
10
from sqlalchemy.ext.declarative import declarative_base
 
11
from dogpile.cache.region import make_region
 
12
import os
 
13
import md5
 
14
 
 
15
# dogpile cache regions.  A home base for cache configurations.
 
16
regions = {}
 
17
 
 
18
 
 
19
# scoped_session.  Apply our custom CachingQuery class to it,
 
20
# using a callable that will associate the dictionary
 
21
# of regions with the Query.
 
22
Session = scoped_session(
 
23
                sessionmaker(
 
24
                    query_cls=caching_query.query_callable(regions)
 
25
                )
 
26
            )
 
27
 
 
28
# global declarative base class.
 
29
Base = declarative_base()
 
30
 
 
31
root = "./dogpile_data/"
 
32
 
 
33
if not os.path.exists(root):
 
34
    raw_input("Will create datafiles in %r.\n"
 
35
                "To reset the cache + database, delete this directory.\n"
 
36
                "Press enter to continue.\n" % root
 
37
                )
 
38
    os.makedirs(root)
 
39
 
 
40
dbfile = os.path.join(root, "dogpile_demo.db")
 
41
engine = create_engine('sqlite:///%s' % dbfile, echo=True)
 
42
Session.configure(bind=engine)
 
43
 
 
44
 
 
45
def md5_key_mangler(key):
 
46
    """Receive cache keys as long concatenated strings;
 
47
    distill them into an md5 hash.
 
48
 
 
49
    """
 
50
    return md5.md5(key).hexdigest()
 
51
 
 
52
# configure the "default" cache region.
 
53
regions['default'] = make_region(
 
54
            # the "dbm" backend needs
 
55
            # string-encoded keys
 
56
            key_mangler=md5_key_mangler
 
57
        ).configure(
 
58
        # using type 'file' to illustrate
 
59
        # serialized persistence.  Normally
 
60
        # memcached or similar is a better choice
 
61
        # for caching.
 
62
        'dogpile.cache.dbm',
 
63
        expiration_time=3600,
 
64
        arguments={
 
65
            "filename": os.path.join(root, "cache.dbm")
 
66
        }
 
67
    )
 
68
 
 
69
# optional; call invalidate() on the region
 
70
# once created so that all data is fresh when
 
71
# the app is restarted.  Good for development,
 
72
# on a production system needs to be used carefully
 
73
# regions['default'].invalidate()
 
74
 
 
75
 
 
76
installed = False
 
77
 
 
78
def bootstrap():
 
79
    global installed
 
80
    import fixture_data
 
81
    if not os.path.exists(dbfile):
 
82
        fixture_data.install()
 
83
        installed = True
 
 
b'\\ No newline at end of file'