Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: third_party/google-endpoints/dogpile/cache/plugins/mako_cache.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 """
2 Mako Integration
3 ----------------
4
5 dogpile.cache includes a `Mako <http://www.makotemplates.org>`_ plugin
6 that replaces `Beaker <http://beaker.groovie.org>`_
7 as the cache backend.
8 Setup a Mako template lookup using the "dogpile.cache" cache implementation
9 and a region dictionary::
10
11 from dogpile.cache import make_region
12 from mako.lookup import TemplateLookup
13
14 my_regions = {
15 "local":make_region().configure(
16 "dogpile.cache.dbm",
17 expiration_time=360,
18 arguments={"filename":"file.dbm"}
19 ),
20 "memcached":make_region().configure(
21 "dogpile.cache.pylibmc",
22 expiration_time=3600,
23 arguments={"url":["127.0.0.1"]}
24 )
25 }
26
27 mako_lookup = TemplateLookup(
28 directories=["/myapp/templates"],
29 cache_impl="dogpile.cache",
30 cache_args={
31 'regions':my_regions
32 }
33 )
34
35 To use the above configuration in a template, use the ``cached=True``
36 argument on any Mako tag which accepts it, in conjunction with the
37 name of the desired region as the ``cache_region`` argument::
38
39 <%def name="mysection()" cached="True" cache_region="memcached">
40 some content that's cached
41 </%def>
42
43
44 """
45 from mako.cache import CacheImpl
46
47
48 class MakoPlugin(CacheImpl):
49 """A Mako ``CacheImpl`` which talks to dogpile.cache."""
50
51 def __init__(self, cache):
52 super(MakoPlugin, self).__init__(cache)
53 try:
54 self.regions = self.cache.template.cache_args['regions']
55 except KeyError:
56 raise KeyError(
57 "'cache_regions' argument is required on the "
58 "Mako Lookup or Template object for usage "
59 "with the dogpile.cache plugin.")
60
61 def _get_region(self, **kw):
62 try:
63 region = kw['region']
64 except KeyError:
65 raise KeyError(
66 "'cache_region' argument must be specified with 'cache=True'"
67 "within templates for usage with the dogpile.cache plugin.")
68 try:
69 return self.regions[region]
70 except KeyError:
71 raise KeyError("No such region '%s'" % region)
72
73 def get_and_replace(self, key, creation_function, **kw):
74 expiration_time = kw.pop("timeout", None)
75 return self._get_region(**kw).get_or_create(
76 key, creation_function,
77 expiration_time=expiration_time)
78
79 def get_or_create(self, key, creation_function, **kw):
80 return self.get_and_replace(key, creation_function, **kw)
81
82 def put(self, key, value, **kw):
83 self._get_region(**kw).put(key, value)
84
85 def get(self, key, **kw):
86 expiration_time = kw.pop("timeout", None)
87 return self._get_region(**kw).get(key, expiration_time=expiration_time)
88
89 def invalidate(self, key, **kw):
90 self._get_region(**kw).delete(key)
OLDNEW
« no previous file with comments | « third_party/google-endpoints/dogpile/cache/plugins/__init__.py ('k') | third_party/google-endpoints/dogpile/cache/proxy.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698