Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Looked up the template_fragments cache at runtime.

  • Loading branch information...
commit 3380495e93f5e81b80a251b03ddb0a80b17685f5 1 parent 905a74f
Aymeric Augustin authored November 23, 2013
19  django/templatetags/cache.py
... ...
@@ -1,16 +1,11 @@
1 1
 from __future__ import unicode_literals
2 2
 
3  
-from django.core.cache import caches, InvalidCacheBackendError
  3
+from django.core.cache import cache, caches, InvalidCacheBackendError
4 4
 from django.core.cache.utils import make_template_fragment_key
5 5
 from django.template import Library, Node, TemplateSyntaxError, VariableDoesNotExist
6 6
 
7 7
 register = Library()
8 8
 
9  
-try:
10  
-    default_cache = caches['template_fragments']
11  
-except InvalidCacheBackendError:
12  
-    from django.core.cache import cache as default_cache
13  
-
14 9
 
15 10
 class CacheNode(Node):
16 11
     def __init__(self, nodelist, expire_time_var, fragment_name, vary_on, cache_name):
@@ -35,17 +30,21 @@ def render(self, context):
35 30
             except VariableDoesNotExist:
36 31
                 raise TemplateSyntaxError('"cache" tag got an unknown variable: %r' % self.cache_name.var)
37 32
             try:
38  
-                cache = caches[cache_name]
  33
+                fragment_cache = caches[cache_name]
39 34
             except InvalidCacheBackendError:
40 35
                 raise TemplateSyntaxError('Invalid cache name specified for cache tag: %r' % cache_name)
41 36
         else:
42  
-            cache = default_cache
  37
+            try:
  38
+                fragment_cache = caches['template_fragments']
  39
+            except InvalidCacheBackendError:
  40
+                fragment_cache = caches['default']
  41
+
43 42
         vary_on = [var.resolve(context) for var in self.vary_on]
44 43
         cache_key = make_template_fragment_key(self.fragment_name, vary_on)
45  
-        value = cache.get(cache_key)
  44
+        value = fragment_cache.get(cache_key)
46 45
         if value is None:
47 46
             value = self.nodelist.render(context)
48  
-            cache.set(cache_key, value, expire_time)
  47
+            fragment_cache.set(cache_key, value, expire_time)
49 48
         return value
50 49
 
51 50
 
2  tests/template_tests/tests.py
@@ -514,7 +514,7 @@ def test_cache_fragment_cache(self):
514 514
         o2 = t2.render(ctx)
515 515
 
516 516
         self.assertEqual(o1, 'foo')
517  
-        self.assertNotEqual(o1, o2)
  517
+        self.assertEqual(o2, 'bar')
518 518
 
519 519
     def test_cache_missing_backend(self):
520 520
         """

0 notes on commit 3380495

Please sign in to comment.
Something went wrong with that request. Please try again.