如何在Django中设置Memcached检索超时

时间:2015-07-24 14:31:34

标签: python django caching memcached amazon-elasticache

如何更改Django中Memcached/Elasticache caching backend的检索超时?

我使用亚马逊的Elasticache在Django中缓存内容,而且我经常看到如下错误:

  File "/usr/local/myproject/.env/lib/python2.7/site-packages/django/template/defaulttags.py", line 285, in render
    return nodelist.render(context)
  File "/usr/local/myproject/.env/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/usr/local/myproject/.env/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
    return node.render(context)
  File "/usr/local/myproject/.env/lib/python2.7/site-packages/django/templatetags/static.py", line 109, in render
    url = self.url(context)
  File "/usr/local/myproject/.env/lib/python2.7/site-packages/django/contrib/staticfiles/templatetags/staticfiles.py", line 12, in url
    return staticfiles_storage.url(path)
  File "/usr/local/myproject/.env/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 136, in url
    hashed_name = self.cache.get(cache_key)
  File "/usr/local/myproject/.env/lib/python2.7/site-packages/django/core/cache/backends/memcached.py", line 64, in get
    val = self._cache.get(key)
Error: error 31 from memcached_get(myproject:1:staticfiles:27e4bc0): A TIMEOUT OCCURRED

我尝试增加Elasticache群集中的节点数量,但这没有任何效果。我的下一个想法是增加memcached检索的超时,但Django docs似乎没有为此提供选项。

" TIMEOUT"选项,但似乎定义了内容到期之前的默认时间,而不是对memcached服务器的HTTP请求的超时。

2 个答案:

答案 0 :(得分:6)

我使用的解决方案是将我的Django缓存后端切换到django-ft-cache,这是标准memcache后端的容错版本。所以现在,当发生周期性超时时,缓存只是绕过非缓存检索媒体而不是抛出500错误。

答案 1 :(得分:4)

django没有设置这样做。 这样的东西应该有效,尽管它很脏。确保在创建缓存之前执行以下操作:

import memcached; memcached._SOCKET_TIMEOUT = whatever_you_want_it_to_be;