Skip to content

Commit

Permalink
merging extra-cache-tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Balogh committed Aug 13, 2010
2 parents 4fe5dd1 + 44a3b07 commit 89ce18e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
22 changes: 15 additions & 7 deletions caching/ext.py
@@ -1,4 +1,5 @@
from django.conf import settings
from django.utils import encoding

from jinja2 import nodes
from jinja2.ext import Extension
Expand Down Expand Up @@ -40,10 +41,16 @@ def parse(self, parser):

# If there is a comma, the user provided a timeout. If not, use
# None as second parameter.
if parser.stream.skip_if('comma'):
args.append(parser.parse_expression())
else:
args.append(nodes.Const(None))
timeout = nodes.Const(None)
extra = nodes.Const([])
while parser.stream.skip_if('comma'):
x = parser.parse_expression()
if parser.stream.current.type == 'assign':
next(parser.stream)
extra = parser.parse_expression()
else:
timeout = x
args.extend([timeout, extra])

body = parser.parse_statements(['name:endcache'], drop_needle=True)

Expand All @@ -58,12 +65,13 @@ def process_cache_arguments(self, args):
"""Extension point for adding anything extra to the cache_support."""
pass

def _cache_support(self, name, obj, timeout, caller):
def _cache_support(self, name, obj, timeout, extra, caller):
"""Cache helper callback."""
if settings.TEMPLATE_DEBUG:
return caller()
return caching.base.cached_with(obj, caller, 'fragment:' + name,
timeout)
extra = ':'.join(map(encoding.smart_str, extra))
key = 'fragment:%s:%s' % (name, extra)
return caching.base.cached_with(obj, caller, key, timeout)


# Nice import name.
Expand Down
23 changes: 23 additions & 0 deletions tests/test_cache.py
Expand Up @@ -225,6 +225,29 @@ def check(obj, expected):
addon.save()
check(addon, '1\n17')

def test_jinja_cache_tag_extra(self):
env = jinja2.Environment(extensions=['caching.ext.cache'])
addon = Addon.objects.get(id=1)

template = ('{% cache obj, extra=[obj.key] %}{{ obj.id }}:'
'{{ obj.key }}{% endcache %}')

def check(obj, expected):
t = env.from_string(template)
eq_(t.render(obj=obj), expected)

addon.key = 1
check(addon, '1:1')
addon.key = 2
check(addon, '1:2')

template = ('{% cache obj, 10, extra=[obj.key] %}{{ obj.id }}:'
'{{ obj.key }}{% endcache %}')
addon.key = 1
check(addon, '1:1')
addon.key = 2
check(addon, '1:2')

def test_cached_with(self):
counter = mock.Mock()
def expensive():
Expand Down

0 comments on commit 89ce18e

Please sign in to comment.