inject request.APP into the template fragment cache key
This commit is contained in:
Родитель
5836e39a9d
Коммит
ef5d94c051
|
@ -0,0 +1,22 @@
|
|||
import jinja2.runtime
|
||||
from jinja2 import nodes
|
||||
|
||||
import caching.ext
|
||||
|
||||
|
||||
class FragmentCacheExtension(caching.ext.FragmentCacheExtension):
|
||||
"""Extends the default fragment cache to include request.APP in the key."""
|
||||
|
||||
def process_cache_arguments(self, args):
|
||||
args.append(nodes.Getattr(nodes.ContextReference(), 'request', 'load'))
|
||||
|
||||
def _cache_support(self, name, obj, timeout, request, caller):
|
||||
if isinstance(request, jinja2.runtime.Undefined):
|
||||
key = name
|
||||
else:
|
||||
key = '%s:%s' % (name, request.APP.id)
|
||||
sup = super(FragmentCacheExtension, self)._cache_support
|
||||
return sup(key, obj, timeout, caller)
|
||||
|
||||
|
||||
cache = FragmentCacheExtension
|
|
@ -0,0 +1,22 @@
|
|||
import jingo
|
||||
import mock
|
||||
from nose.tools import eq_
|
||||
|
||||
|
||||
@mock.patch('caching.ext.cache._cache_support')
|
||||
def test_app_in_fragment_cache_key(cache_mock):
|
||||
cache_mock.return_value = ''
|
||||
request = mock.Mock()
|
||||
request.APP.id = '<app>'
|
||||
request.groups = []
|
||||
template = jingo.env.from_string('{% cache 1 %}{% endcache %}')
|
||||
jingo.render(request, template)
|
||||
assert cache_mock.call_args[0][0].endswith('<app>')
|
||||
|
||||
|
||||
@mock.patch('caching.ext.cache._cache_support')
|
||||
def test_fragment_cache_key_no_app(cache_mock):
|
||||
cache_mock.return_value = 'xx'
|
||||
template = jingo.env.from_string('{% cache 1 %}{% endcache %}')
|
||||
eq_(template.render(), 'xx')
|
||||
assert cache_mock.called
|
|
@ -164,7 +164,7 @@ def JINJA_CONFIG():
|
|||
import jinja2
|
||||
from django.conf import settings
|
||||
from caching.base import cache
|
||||
config = {'extensions': ['tower.template.i18n', 'caching.ext.cache',
|
||||
config = {'extensions': ['tower.template.i18n', 'amo.ext.cache',
|
||||
'jinja2.ext.with_', 'jinja2.ext.loopcontrols'],
|
||||
'finalize': lambda x: x if x is not None else ''}
|
||||
if 'memcached' in cache.scheme and not settings.DEBUG:
|
||||
|
|
Загрузка…
Ссылка в новой задаче