Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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: http://code.djangoproject.com/svn/django/trunk@8418 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2d2396a3841d56243e9caab39fdb57dead17f5c4 1 parent f505bd6
Malcolm Tredinnick authored August 16, 2008
8  django/core/cache/__init__.py
@@ -17,6 +17,7 @@
17 17
 
18 18
 from cgi import parse_qsl
19 19
 from django.conf import settings
  20
+from django.core import signals
20 21
 from django.core.cache.backends.base import InvalidCacheBackendError
21 22
 
22 23
 # Name for use in settings file --> name of module in "backends" directory.
@@ -54,3 +55,10 @@ def get_cache(backend_uri):
54 55
     return getattr(module, 'CacheClass')(host, params)
55 56
 
56 57
 cache = get_cache(settings.CACHE_BACKEND)
  58
+
  59
+# Some caches -- pythont-memcached in particular -- need to do a cleanup at the
  60
+# end of a request cycle. If the cache provides a close() method, wire it up
  61
+# here.
  62
+if hasattr(cache, 'close'):
  63
+    signals.request_finished.connect(cache.close)
  64
+
4  django/core/cache/backends/memcached.py
@@ -39,3 +39,7 @@ def delete(self, key):
39 39
 
40 40
     def get_many(self, keys):
41 41
         return self._cache.get_multi(map(smart_str,keys))
  42
+
  43
+    def close(self, **kwargs):
  44
+        self._cache.disconnect_all()
  45
+

0 notes on commit 2d2396a

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