diff options
Diffstat (limited to 'mako')
-rw-r--r-- | mako/cache.py | 28 | ||||
-rw-r--r-- | mako/codegen.py | 4 |
2 files changed, 22 insertions, 10 deletions
diff --git a/mako/cache.py b/mako/cache.py index 959640e..34736ce 100644 --- a/mako/cache.py +++ b/mako/cache.py @@ -78,12 +78,18 @@ class Cache(object): """Retrieve a value from the cache, using the given creation function to generate a new value.""" + return self._ctx_get_or_create(key, creation_function, None, **kw) + + def _ctx_get_or_create(self, key, creation_function, context, **kw): + """Retrieve a value from the cache, using the given creation function + to generate a new value.""" + if not self.template.cache_enabled: return creation_function() return self.impl.get_or_create(key, creation_function, - **self._get_cache_kw(kw)) + **self._get_cache_kw(kw, context)) def set(self, key, value, **kw): """Place a value in the cache. @@ -94,7 +100,7 @@ class Cache(object): """ - self.impl.set(key, value, **self._get_cache_kw(kw)) + self.impl.set(key, value, **self._get_cache_kw(kw, None)) put = set """A synonym for :meth:`.Cache.set`. @@ -113,7 +119,7 @@ class Cache(object): values will use that same backend. """ - return self.impl.get(key, **self._get_cache_kw(kw)) + return self.impl.get(key, **self._get_cache_kw(kw, None)) def invalidate(self, key, **kw): """Invalidate a value in the cache. @@ -125,7 +131,7 @@ class Cache(object): values will use that same backend. """ - self.impl.invalidate(key, **self._get_cache_kw(kw)) + self.impl.invalidate(key, **self._get_cache_kw(kw, None)) def invalidate_body(self): """Invalidate the cached content of the "body" method for this template. @@ -151,19 +157,21 @@ class Cache(object): self.invalidate(name, __M_defname=name) - def _get_cache_kw(self, kw): + def _get_cache_kw(self, kw, context): defname = kw.pop('__M_defname', None) if not defname: tmpl_kw = self.template.cache_args.copy() tmpl_kw.update(kw) - return tmpl_kw elif defname in self._def_regions: - return self._def_regions[defname] + tmpl_kw = self._def_regions[defname] else: tmpl_kw = self.template.cache_args.copy() tmpl_kw.update(kw) self._def_regions[defname] = tmpl_kw - return tmpl_kw + if context and self.impl.pass_context: + tmpl_kw = tmpl_kw.copy() + tmpl_kw.setdefault('context', context) + return tmpl_kw class CacheImpl(object): """Provide a cache implementation for use by :class:`.Cache`.""" @@ -171,6 +179,10 @@ class CacheImpl(object): def __init__(self, cache): self.cache = cache + pass_context = False + """If True, the Context will be passed to get_or_create + as the name 'context'.""" + def get_or_create(self, key, creation_function, **kw): """Retrieve a value from the cache, using the given creation function to generate a new value. diff --git a/mako/codegen.py b/mako/codegen.py index 704330c..51eb01e 100644 --- a/mako/codegen.py +++ b/mako/codegen.py @@ -635,7 +635,7 @@ class _GenerateRenderMethod(object): ) if buffered: s = "context.get('local')."\ - "cache.get_or_create(%s, lambda:__M_%s(%s), %s__M_defname=%r)" % \ + "cache._ctx_get_or_create(%s, lambda:__M_%s(%s), context, %s__M_defname=%r)" % \ (cachekey, name, ','.join(pass_args), ''.join(["%s=%s, " % (k,v) for k, v in cache_args.items()]), name @@ -646,7 +646,7 @@ class _GenerateRenderMethod(object): else: self.printer.writelines( "__M_writer(context.get('local')." - "cache.get_or_create(%s, lambda:__M_%s(%s), %s__M_defname=%r))" % + "cache._ctx_get_or_create(%s, lambda:__M_%s(%s), context, %s__M_defname=%r))" % (cachekey, name, ','.join(pass_args), ''.join(["%s=%s, " % (k,v) for k, v in cache_args.items()]), name, |