Django custom cache_page decorater returns error on only first 运行 after it's ok
Django custom cache_page decorater returns error on the only first run afterwards it's ok
我为我的应用创建了自定义 cache_page
装饰器。它不适用于第一个 运行 并抛出与中间件相关的错误:
content_encoding = response.get("Content-Encoding", "")
AttributeError: 'bool' object has no attribute 'get'
但是在第二次和更远的 运行 上,它起作用了,因为已经设置了缓存。我安装了 django debug_toolbar 并将 cors 中间件添加到我的中间件中。任何人都可以帮忙吗?这是我的自定义装饰器函数:
def cache_page(timeout):
"""custom cache page decorator"""
def decorator(func):
@wraps(func)
def wrapper(request, *args, **kwargs):
print("wrapp", request)
cache_key = hashlib.md5(
iri_to_uri(request.build_absolute_uri()).encode('ascii')
).hexdigest()
cached_data = cache.get(cache_key)
if cached_data is not None:
return cached_data
response = func(request, *args, **kwargs)
if (isinstance(response, Response) and response.status_code in (200, 301, 302, 304)):
cache_timeout = timeout() if callable(timeout) else timeout
if hasattr(response, 'render') and callable(response.render):
response.add_post_render_callback(
lambda r: cache.set(cache_key, r, cache_timeout)
)
else:
cache.set(cache_key, response, cache_timeout)
return response
return wrapper
return decorator
使用 Django 2.2b 测试:
我使用了这个 Mixin:
class CacheKeyDispatchMixin:
def dispatch(self, *args, **kwargs):
if self.request.method == 'GET' or self.request.method == 'HEAD':
url_to_cache = '/{0}{1}'.format(get_language(), self.request.get_full_path())
cache_hash = calculate_xxxhash(url_to_cache)
data = cache.get(cache_hash)
if not data:
response = super(CacheKeyDispatchMixin, self).dispatch(*args, **kwargs)
if response.status_code == 200:
response.render()
cache.set(cache_hash, response)
logger.info('Cache added {0} ({1})'.format(url_to_cache, cache_hash))
return response
logger.info('Cache hit {0} ({1}).'.format(url_to_cache, cache_hash))
return data
return super(CacheKeyDispatchMixin, self).dispatch(*args, **kwargs)
基本上你可以在缓存之前调用 render()
。
我为我的应用创建了自定义 cache_page
装饰器。它不适用于第一个 运行 并抛出与中间件相关的错误:
content_encoding = response.get("Content-Encoding", "")
AttributeError: 'bool' object has no attribute 'get'
但是在第二次和更远的 运行 上,它起作用了,因为已经设置了缓存。我安装了 django debug_toolbar 并将 cors 中间件添加到我的中间件中。任何人都可以帮忙吗?这是我的自定义装饰器函数:
def cache_page(timeout):
"""custom cache page decorator"""
def decorator(func):
@wraps(func)
def wrapper(request, *args, **kwargs):
print("wrapp", request)
cache_key = hashlib.md5(
iri_to_uri(request.build_absolute_uri()).encode('ascii')
).hexdigest()
cached_data = cache.get(cache_key)
if cached_data is not None:
return cached_data
response = func(request, *args, **kwargs)
if (isinstance(response, Response) and response.status_code in (200, 301, 302, 304)):
cache_timeout = timeout() if callable(timeout) else timeout
if hasattr(response, 'render') and callable(response.render):
response.add_post_render_callback(
lambda r: cache.set(cache_key, r, cache_timeout)
)
else:
cache.set(cache_key, response, cache_timeout)
return response
return wrapper
return decorator
使用 Django 2.2b 测试:
我使用了这个 Mixin:
class CacheKeyDispatchMixin:
def dispatch(self, *args, **kwargs):
if self.request.method == 'GET' or self.request.method == 'HEAD':
url_to_cache = '/{0}{1}'.format(get_language(), self.request.get_full_path())
cache_hash = calculate_xxxhash(url_to_cache)
data = cache.get(cache_hash)
if not data:
response = super(CacheKeyDispatchMixin, self).dispatch(*args, **kwargs)
if response.status_code == 200:
response.render()
cache.set(cache_hash, response)
logger.info('Cache added {0} ({1})'.format(url_to_cache, cache_hash))
return response
logger.info('Cache hit {0} ({1}).'.format(url_to_cache, cache_hash))
return data
return super(CacheKeyDispatchMixin, self).dispatch(*args, **kwargs)
基本上你可以在缓存之前调用 render()
。