Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Fixed #5133 -- Explicitly close memcached connections after each request

(similar to database connection management). We can't effectively manage the
lifecycle by pooling connections and recent versions of python-memcache can
lead to connection exhaustion in some quite reasonable setups.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2d2396a3841d56243e9caab39fdb57dead17f5c4 1 parent f505bd6
@malcolmt malcolmt authored
8 django/core/cache/
@@ -17,6 +17,7 @@
from cgi import parse_qsl
from django.conf import settings
+from django.core import signals
from django.core.cache.backends.base import InvalidCacheBackendError
# Name for use in settings file --> name of module in "backends" directory.
@@ -54,3 +55,10 @@ def get_cache(backend_uri):
return getattr(module, 'CacheClass')(host, params)
cache = get_cache(settings.CACHE_BACKEND)
+# Some caches -- pythont-memcached in particular -- need to do a cleanup at the
+# end of a request cycle. If the cache provides a close() method, wire it up
+# here.
+if hasattr(cache, 'close'):
+ signals.request_finished.connect(cache.close)
4 django/core/cache/backends/
@@ -39,3 +39,7 @@ def delete(self, key):
def get_many(self, keys):
return self._cache.get_multi(map(smart_str,keys))
+ def close(self, **kwargs):
+ self._cache.disconnect_all()

0 comments on commit 2d2396a

Please sign in to comment.
Something went wrong with that request. Please try again.