From 3380495e93f5e81b80a251b03ddb0a80b17685f5 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 23 Nov 2013 14:18:07 +0100 Subject: [PATCH] Looked up the template_fragments cache at runtime. --- django/templatetags/cache.py | 19 +++++++++---------- tests/template_tests/tests.py | 2 +- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/django/templatetags/cache.py b/django/templatetags/cache.py index 7d03b49b6d..bdea9b59ae 100644 --- a/django/templatetags/cache.py +++ b/django/templatetags/cache.py @@ -1,16 +1,11 @@ from __future__ import unicode_literals -from django.core.cache import caches, InvalidCacheBackendError +from django.core.cache import cache, caches, InvalidCacheBackendError from django.core.cache.utils import make_template_fragment_key from django.template import Library, Node, TemplateSyntaxError, VariableDoesNotExist register = Library() -try: - default_cache = caches['template_fragments'] -except InvalidCacheBackendError: - from django.core.cache import cache as default_cache - class CacheNode(Node): def __init__(self, nodelist, expire_time_var, fragment_name, vary_on, cache_name): @@ -35,17 +30,21 @@ class CacheNode(Node): except VariableDoesNotExist: raise TemplateSyntaxError('"cache" tag got an unknown variable: %r' % self.cache_name.var) try: - cache = caches[cache_name] + fragment_cache = caches[cache_name] except InvalidCacheBackendError: raise TemplateSyntaxError('Invalid cache name specified for cache tag: %r' % cache_name) else: - cache = default_cache + try: + fragment_cache = caches['template_fragments'] + except InvalidCacheBackendError: + fragment_cache = caches['default'] + vary_on = [var.resolve(context) for var in self.vary_on] cache_key = make_template_fragment_key(self.fragment_name, vary_on) - value = cache.get(cache_key) + value = fragment_cache.get(cache_key) if value is None: value = self.nodelist.render(context) - cache.set(cache_key, value, expire_time) + fragment_cache.set(cache_key, value, expire_time) return value diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index bad15e2834..f54fc6fcba 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -514,7 +514,7 @@ class TemplateRegressionTests(TestCase): o2 = t2.render(ctx) self.assertEqual(o1, 'foo') - self.assertNotEqual(o1, o2) + self.assertEqual(o2, 'bar') def test_cache_missing_backend(self): """