简体   繁体   English

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

[英]How to set Memcached retrieval timeout in Django

How do you change the retrieval timeout for the Memcached/Elasticache caching backend in Django? 如何在Django中更改Memcached / Elasticache缓存后端的检索超时?

I'm using Amazon's Elasticache for caching content in Django, and I'm frequently seeing errors like: 我正在使用亚马逊的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

I've tried increasing the number of nodes in my Elasticache cluster, but that has had no effect. 我已经尝试增加我的Elasticache集群中的节点数量,但这没有任何效果。 My next thought was to increase the timeout for the memcached retrieval, but the Django docs don't seem to provide an option for this. 我的下一个想法是增加memcached检索的超时,但Django文档似乎没有为此提供选项。

There's a "TIMEOUT" option, but that seems to define the default time after which the content expires , not the timeout of the HTTP request to the memcached server. 有一个“TIMEOUT”选项,但这似乎定义了内容到期之前的默认时间,而不是HTTP请求到memcached服务器的超时。

The solution I went with was to switch my Django cache backend to django-ft-cache , a fault-tolerant version of the standard memcache backend. 我使用的解决方案是将我的Django缓存后端切换到django-ft-cache ,这是标准memcache后端的容错版本。 So now, when a periodic timeout occurs, the cache simply bypasses to a non-cache retrieval of media instead of throwing a 500 error. 所以现在,当发生周期性超时时,缓存只是绕过非缓存检索媒体而不是抛出500错误。

There is no setting in django to do that. django没有设置这样做。 Something like this should work, albeit it is quite dirty. 这样的东西应该有效,尽管它很脏。 Make sure following is executed before cache is created: 确保在创建缓存之前执行以下操作:

import memcached; memcached._SOCKET_TIMEOUT = whatever_you_want_it_to_be;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM