How do you change the retrieval timeout for the Memcached/Elasticache caching backend in Django?
I'm using Amazon's Elasticache for caching content in Django, and I'm frequently seeing errors like:
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. 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.
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.
This answer explains how to install Memcached on Windows 10 and how to integrate it with Django through a specific client. It was validated using Memcached 1.4. 4, Python 2.7 and Django 1.11. Go to the Django project, start the server and you should get much better results in your Time load.
To use cache in Django, first thing to do is to set up where the cache will stay. The cache framework offers different possibilities - cache can be saved in database, on file system or directly in memory. Setting is done in the settings.py file of your project.
Unless we explicitly specify another caching method in our settings file, Django defaults to local memory caching. As its name implies, this method stores cached data in RAM on the machine where Django is running. Local memory caching is fast, responsive, and thread-safe.
Open the admin interface and find the new menu item “Clear Memcache”. There you can clear cache items for the configured KEY_PREFIX or even the whole Memcache server.
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. So now, when a periodic timeout occurs, the cache simply bypasses to a non-cache retrieval of media instead of throwing a 500 error.
There is no setting in django to do that. 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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With