嘿伙计们,我刚刚在我的一些代码中实现了Django Cache Local Memory后端,但它似乎导致了一个问题。
尝试查看站点时出现以下错误(启用调试):
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 279, in run
self.result = application(self.environ, self.start_response)
File "/usr/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 651, in __call__
return self.application(environ, start_response)
File "/usr/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 245, in __call__
response = middleware_method(request, response)
File "/usr/lib/python2.6/dist-packages/django/middleware/cache.py", line 91, in process_response
patch_response_headers(response, timeout)
File "/usr/lib/python2.6/dist-packages/django/utils/cache.py", line 112, in patch_response_headers
response['Expires'] = http_date(time.time() + cache_timeout)
TypeError: unsupported operand type(s) for +: 'float' and 'str'
我检查了我的代码,因为缓存一切似乎都没问题。例如,我的中间件中有以下内容。
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)
我的缓存设置:
CACHE_BACKEND = 'locmem://'
CACHE_MIDDLEWARE_SECONDS = '3600'
CACHE_MIDDLEWARE_KEY_PREFIX = 'za'
CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
我的一些代码(模板标签):
def get_featured_images():
"""
provides featured images
"""
cache_key = 'featured_images'
images = cache.get(cache_key)
if images is None:
images = FeaturedImage.objects.all().filter(enabled=True)[:5]
cache.set(cache_key, images)
return {'images': images}
任何想法可能是什么问题,从下面的错误信息看起来django的cache.py中存在问题?
更新!!!将时间缓存设置为字符串!
答案 0 :(得分:4)
您将缓存的秒数定义为字符串而不是int。应该是:
CACHE_MIDDLEWARE_SECONDS = 3600
答案 1 :(得分:0)
CACHE_MIDDLEWARE_SECONDS
应该是int / float,而不是string:
CACHE_MIDDLEWARE_SECONDS = 3600