mirror of https://github.com/django/django.git
Looked up the template_fragments cache at runtime.
This commit is contained in:
parent
905a74f52b
commit
3380495e93
|
@ -1,16 +1,11 @@
|
||||||
from __future__ import unicode_literals
|
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.core.cache.utils import make_template_fragment_key
|
||||||
from django.template import Library, Node, TemplateSyntaxError, VariableDoesNotExist
|
from django.template import Library, Node, TemplateSyntaxError, VariableDoesNotExist
|
||||||
|
|
||||||
register = Library()
|
register = Library()
|
||||||
|
|
||||||
try:
|
|
||||||
default_cache = caches['template_fragments']
|
|
||||||
except InvalidCacheBackendError:
|
|
||||||
from django.core.cache import cache as default_cache
|
|
||||||
|
|
||||||
|
|
||||||
class CacheNode(Node):
|
class CacheNode(Node):
|
||||||
def __init__(self, nodelist, expire_time_var, fragment_name, vary_on, cache_name):
|
def __init__(self, nodelist, expire_time_var, fragment_name, vary_on, cache_name):
|
||||||
|
@ -35,17 +30,21 @@ class CacheNode(Node):
|
||||||
except VariableDoesNotExist:
|
except VariableDoesNotExist:
|
||||||
raise TemplateSyntaxError('"cache" tag got an unknown variable: %r' % self.cache_name.var)
|
raise TemplateSyntaxError('"cache" tag got an unknown variable: %r' % self.cache_name.var)
|
||||||
try:
|
try:
|
||||||
cache = caches[cache_name]
|
fragment_cache = caches[cache_name]
|
||||||
except InvalidCacheBackendError:
|
except InvalidCacheBackendError:
|
||||||
raise TemplateSyntaxError('Invalid cache name specified for cache tag: %r' % cache_name)
|
raise TemplateSyntaxError('Invalid cache name specified for cache tag: %r' % cache_name)
|
||||||
else:
|
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]
|
vary_on = [var.resolve(context) for var in self.vary_on]
|
||||||
cache_key = make_template_fragment_key(self.fragment_name, 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:
|
if value is None:
|
||||||
value = self.nodelist.render(context)
|
value = self.nodelist.render(context)
|
||||||
cache.set(cache_key, value, expire_time)
|
fragment_cache.set(cache_key, value, expire_time)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -514,7 +514,7 @@ class TemplateRegressionTests(TestCase):
|
||||||
o2 = t2.render(ctx)
|
o2 = t2.render(ctx)
|
||||||
|
|
||||||
self.assertEqual(o1, 'foo')
|
self.assertEqual(o1, 'foo')
|
||||||
self.assertNotEqual(o1, o2)
|
self.assertEqual(o2, 'bar')
|
||||||
|
|
||||||
def test_cache_missing_backend(self):
|
def test_cache_missing_backend(self):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue