我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用django.core.cache.InvalidCacheBackendError()。
def test_bad_cache(self): """The RATELIMIT_USE_CACHE setting works if the cache exists.""" self.skipTest('I do not know why this fails when the other works.') class BadCacheView(RatelimitMixin, View): ratelimit_key = 'ip' def post(self, request, *args, **kwargs): return request get = post view = BadCacheView.as_view() req = rf.post('/') with self.assertRaises(InvalidCacheBackendError): view(req)
def render(self, context): try: expire_time = self.expire_time_var.resolve(context) except VariableDoesNotExist: raise TemplateSyntaxError('"cache" tag got an unknown variable: %r' % self.expire_time_var.var) try: expire_time = int(expire_time) except (ValueError, TypeError): raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time) if self.cache_name: try: cache_name = self.cache_name.resolve(context) except VariableDoesNotExist: raise TemplateSyntaxError('"cache" tag got an unknown variable: %r' % self.cache_name.var) try: fragment_cache = caches[cache_name] except InvalidCacheBackendError: raise TemplateSyntaxError('Invalid cache name specified for cache tag: %r' % cache_name) else: 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 = fragment_cache.get(cache_key) if value is None: value = self.nodelist.render(context) fragment_cache.set(cache_key, value, expire_time) return value
def test_bad_cache(self): """The RATELIMIT_USE_CACHE setting works if the cache exists.""" @ratelimit(key='ip', rate='1/m') def view(request): return request req = rf.post('/') with self.assertRaises(InvalidCacheBackendError): view(req)
def cache_it(expires=None, key_func=None): if not expires: expires = getattr(settings, 'VIMEO_CACHE_EXPIRES', 300) cache = None try: cache_backend = getattr(settings, 'VIMEO_CACHE_BACKEND', None) cache = caches[cache_backend] except InvalidCacheBackendError as e: logger.warning('Vimeo cache "VIMEO_CACHE_BACKEND" disabled, reason: {}'.format(e)) def wrap(f): @wraps(f) def wrapper(*args, **kwds): if not cache: return f(*args, **kwds) if key_func: key = 'django_vimeo_cache:{}'.format(key_func(*args, **kwds)) else: key = 'django_vimeo_cache:' + f.__name__ + ':' +\ str(list(args) + list(sorted(kwds.items()))) key = xxhash.xxh64(key).hexdigest() value = cache.get(key) if value is None: value = f(*args, **kwds) cache.set(key, value, expires) value = cache.get(key) if value is None: raise Exception('failed to fetch cached value, try again') return value return wrapper return wrap