如何在Django中更改Memcached/Elasticache caching backend的检索超时?
我正在使用Amazon的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请求的超时。
最佳答案
我采用的解决方案是将Django缓存后端切换到django-ft-cache,这是标准内存缓存后端的容错版本。因此,现在,当发生定期超时时,缓存将简单地绕过媒体的非缓存检索,而不会引发500错误。