Skip to content

Commit

Permalink
- cached blocks now use the current context when rendering
Browse files Browse the repository at this point in the history
an expired section, instead of the original context
passed in [ticket:87]
  • Loading branch information
zzzeek committed Jun 23, 2008
1 parent a136efc commit 78884f2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
0.2.2
- cached blocks now use the current context when rendering
an expired section, instead of the original context
passed in [ticket:87]
- fixed a critical issue regarding caching, whereby
a cached block would raise an error when called within a
cache-refresh operation that was initiated after the
Expand Down
13 changes: 5 additions & 8 deletions lib/mako/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ def put(self, key, value, type='memory', **kwargs):
def get(self, key, type='memory', **kwargs):
return self._get_container(key, type, **kwargs).get_value()
def _get_container(self, key, type, **kwargs):
try:
return self._containers[key]
except KeyError:
if container is None:
raise exceptions.RuntimeException("the Beaker package is required to use cache functionality.")
kw = self.kwargs.copy()
kw.update(kwargs)
return self._containers.setdefault(key, clsmap[type](key, self.context, self.id, starttime=self.starttime, **kw))
if not container:
raise exceptions.RuntimeException("the Beaker package is required to use cache functionality.")
kw = self.kwargs.copy()
kw.update(kwargs)
return clsmap[type](key, self.context, self.id, starttime=self.starttime, **kw)

6 changes: 3 additions & 3 deletions lib/mako/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,9 @@ def write_def_finish(self, node, buffered, filtered, cached, callstack=True):
if buffered or filtered or cached:
if buffered or cached:
# in a caching scenario, don't try to get a writer
# from the context after popping - if the callable
# is called within a cache refresh operation, there's
# no more buffers on the stack
# from the context after popping; assume the caching
# implemenation might be using a context with no
# extra buffers
self.printer.writelines(
"finally:",
"__M_buf = context._pop_buffer()"
Expand Down
16 changes: 16 additions & 0 deletions test/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,22 @@ def test_load_from_expired(self):
time.sleep(3)
x2 = t.render()
assert x1.strip() == x2.strip() == "foo"

def test_cache_uses_current_context(self):
t = Template("""
${foo()}
<%def name="foo()" cached="True" cache_timeout="2">
foo: ${x}
</%def>
""")

import time
x1 = t.render(x=1)
time.sleep(3)
x2 = t.render(x=2)
assert x1.strip() == "foo: 1"
assert x2.strip() == "foo: 2"


def _install_mock_cache(self, template):
m = MockCache(template.module._template_cache)
Expand Down

0 comments on commit 78884f2

Please sign in to comment.