本文整理汇总了Python中django.core.cache.DEFAULT_CACHE_ALIAS属性的典型用法代码示例。如果您正苦于以下问题:Python cache.DEFAULT_CACHE_ALIAS属性的具体用法?Python cache.DEFAULT_CACHE_ALIAS怎么用?Python cache.DEFAULT_CACHE_ALIAS使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类django.core.cache
的用法示例。
在下文中一共展示了cache.DEFAULT_CACHE_ALIAS属性的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cache
# 需要导入模块: from django.core import cache [as 别名]
# 或者: from django.core.cache import DEFAULT_CACHE_ALIAS [as 别名]
def test_cache(self):
other_cache_alias = next(alias for alias in settings.CACHES
if alias != DEFAULT_CACHE_ALIAS)
invalidate(Test, cache_alias=other_cache_alias)
qs = Test.objects.all()
with self.settings(CACHALOT_CACHE=DEFAULT_CACHE_ALIAS):
self.assert_query_cached(qs)
with self.settings(CACHALOT_CACHE=other_cache_alias):
self.assert_query_cached(qs)
Test.objects.create(name='test')
# Only `CACHALOT_CACHE` is invalidated, so changing the database should
# not invalidate all caches.
with self.settings(CACHALOT_CACHE=other_cache_alias):
self.assert_query_cached(qs, before=0)
示例2: setUp
# 需要导入模块: from django.core import cache [as 别名]
# 或者: from django.core.cache import DEFAULT_CACHE_ALIAS [as 别名]
def setUp(self):
super(APITestCase, self).setUp()
self.t1 = Test.objects.create(name='test1')
self.cache_alias2 = next(alias for alias in settings.CACHES
if alias != DEFAULT_CACHE_ALIAS)
示例3: cache
# 需要导入模块: from django.core import cache [as 别名]
# 或者: from django.core.cache import DEFAULT_CACHE_ALIAS [as 别名]
def cache(self, *args, **kwargs):
cache_alias = kwargs.get('cache_alias', DEFAULT_CACHE_ALIAS)
cache_key = kwargs.get('cache_key', kwargs['default_cache_key'])
if cache_key is None:
raise ValueError(
'You must set `cache_key` when the template is not a file.')
cache_key = make_template_fragment_key(cache_key, args)
out = caches[cache_alias].get(cache_key)
if out is None:
out = kwargs['caller']()
caches[cache_alias].set(cache_key, out, kwargs.get('timeout'))
return out
示例4: on_cache_op
# 需要导入模块: from django.core import cache [as 别名]
# 或者: from django.core.cache import DEFAULT_CACHE_ALIAS [as 别名]
def on_cache_op(self, cache_op):
name_parts = ["cache"]
if cache_op.alias != DEFAULT_CACHE_ALIAS:
name_parts.append(cache_op.alias)
name_parts.append(cache_op.operation)
name = "|".join(name_parts)
self.record.append({name: cache_op.key_or_keys})
示例5: __repr__
# 需要导入模块: from django.core import cache [as 别名]
# 或者: from django.core.cache import DEFAULT_CACHE_ALIAS [as 别名]
def __repr__(self):
name = type(self._cache)
try:
from django.core.cache import DEFAULT_CACHE_ALIAS, caches, DefaultCacheProxy
if isinstance(self._cache, DefaultCacheProxy):
name = type(caches[DEFAULT_CACHE_ALIAS])
except Exception:
pass
return 'ThreadLocalCache {}'.format(name)
示例6: test_cache_jinja2
# 需要导入模块: from django.core import cache [as 别名]
# 或者: from django.core.cache import DEFAULT_CACHE_ALIAS [as 别名]
def test_cache_jinja2(self):
# Invalid arguments
with self.assertRaises(TemplateSyntaxError,
msg="'invalid' is not a valid keyword argument "
"for {% cache %}"):
engines['jinja2'].from_string("""
{% cache cache_key='anything', invalid='what?' %}{% endcache %}
""")
with self.assertRaises(ValueError, msg='You must set `cache_key` when '
'the template is not a file.'):
engines['jinja2'].from_string(
'{% cache %} broken {% endcache %}').render()
# With the minimum number of arguments
template = engines['jinja2'].from_string("""
{%- cache cache_key='first' -%}
{{ content1 }}
{%- endcache -%}
{%- cache cache_key='second' -%}
{{ content2 }}
{%- endcache -%}
""")
content = template.render({'content1': 'abc', 'content2': 'def'})
self.assertEqual(content, 'abcdef')
invalidate()
content = template.render({'content1': 'ghi', 'content2': 'jkl'})
self.assertEqual(content, 'abcdef')
# With the maximum number of arguments
template = engines['jinja2'].from_string("""
{%- cache get_last_invalidation('auth.Group', 'cachalot_test',
cache_alias=cache),
timeout=10, cache_key='cache_key_name', cache_alias=cache -%}
{{ content }}
{%- endcache -%}
""")
content = template.render({'content': 'something',
'cache': self.cache_alias2})
self.assertEqual(content, 'something')
content = template.render({'content': 'anything',
'cache': self.cache_alias2})
self.assertEqual(content, 'something')
invalidate('cachalot_test', cache_alias=DEFAULT_CACHE_ALIAS)
content = template.render({'content': 'yet another',
'cache': self.cache_alias2})
self.assertEqual(content, 'something')
invalidate('cachalot_test')
content = template.render({'content': 'will you change?',
'cache': self.cache_alias2})
self.assertEqual(content, 'will you change?')
caches[self.cache_alias2].clear()
content = template.render({'content': 'better!',
'cache': self.cache_alias2})
self.assertEqual(content, 'better!')