diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-10-01 17:59:24 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-10-01 17:59:24 -0400 |
commit | a6b60799419dd5d304ad2203f6309157bd73c240 (patch) | |
tree | 60ca4549a165a8594e907b6faced38909415bc9f | |
parent | 7cba856e51fc20eb8012c8b00b56d34a94264119 (diff) | |
download | mako-a6b60799419dd5d304ad2203f6309157bd73c240.tar.gz |
need this too
-rw-r--r-- | mako/ext/beaker_cache.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/mako/ext/beaker_cache.py b/mako/ext/beaker_cache.py new file mode 100644 index 0000000..1d0c26c --- /dev/null +++ b/mako/ext/beaker_cache.py @@ -0,0 +1,57 @@ +"""Provide a :class:`.CacheImpl` for the Beaker caching system.""" + +from mako import exceptions + +from mako.cache import CacheImpl + +_beaker_cache = None +class BeakerCacheImpl(CacheImpl): + """A :class:`.CacheImpl` provided for the Beaker caching system. + + This plugin is used by default, based on the default + value of ``'beaker'`` for the ``cache_impl`` parameter of the + :class:`.Template` or :class:`.TemplateLookup` classes. + + """ + + def __init__(self, cache): + global _beaker_cache + if _beaker_cache is None: + try: + from beaker import cache as beaker_cache + except ImportError, e: + raise exceptions.RuntimeException( + "the Beaker package is required to use cache " + "functionality.") + + _beaker_cache = beaker_cache.CacheManager() + super(BeakerCacheImpl, self).__init__(cache) + + def _get_cache(self, **kw): + expiretime = kw.pop('timeout', None) + if 'dir' in kw: + kw['data_dir'] = kw.pop('dir') + elif self.cache.template.module_directory: + kw['data_dir'] = self.cache.template.module_directory + + if kw.get('type') == 'memcached': + kw['type'] = 'ext:memcached' + + return _beaker_cache.get_cache(self.cache.id, **kw), \ + {'expiretime':expiretime, 'starttime':self.cache.starttime} + + def get_and_replace(self, key, creation_function, **kw): + cache, kw = self._get_cache(**kw) + return cache.get(key, createfunc=creation_function, **kw) + + def put(self, key, value, **kw): + cache, kw = self._get_cache(**kw) + cache.put(key, value, **kw) + + def get(self, key, **kw): + cache, kw = self._get_cache(**kw) + return cache.get(key, **kw) + + def invalidate(self, key, **kw): + cache, kw = self._get_cache(**kw) + cache.remove_value(key, **kw) |