开发者

Django caching seems to be causing problems

开发者 https://www.devze.com 2022-12-29 01:55 出处:网络
Hey guys, i have just implemented the Django Cache Local Memory back end in some my code, however it seems to be causing a problem.

Hey guys, i have just implemented the Django Cache Local Memory back end in some my code, however it seems to be causing a problem.

I get the following error when trying to view the site (With Debug On):

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'

I have checked my code, for caching everything seems to be ok. For e开发者_StackOverflow中文版xample, i have the following in my middleware.

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',
)

My settings for Cache:

CACHE_BACKEND = 'locmem://'
CACHE_MIDDLEWARE_SECONDS = '3600'
CACHE_MIDDLEWARE_KEY_PREFIX = 'za'
CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True

And some of my code (template tag):

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}

Any idea what could be the problem, from the error message below it looks like there's an issue in django's cache.py?

Update!!! Set the time cache to a string!


You defined the number of seconds to cache as a string instead of an int. Should be:

CACHE_MIDDLEWARE_SECONDS = 3600


CACHE_MIDDLEWARE_SECONDS should be int/float, not string:

CACHE_MIDDLEWARE_SECONDS = 3600
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号