summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-08-16 23:35:58 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-08-16 23:35:58 +0000
commit2d2396a3841d56243e9caab39fdb57dead17f5c4 (patch)
tree16092314ec6c89b8a5fef0f8cd9c0b1e58375141
parentf505bd6e415fc771c920b19432dd10941a743097 (diff)
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
-rw-r--r--django/core/cache/__init__.py8
-rw-r--r--django/core/cache/backends/memcached.py4
2 files changed, 12 insertions, 0 deletions
diff --git a/django/core/cache/__init__.py b/django/core/cache/__init__.py
index c136ce4f4d..93e7adb76e 100644
--- a/django/core/cache/__init__.py
+++ b/django/core/cache/__init__.py
@@ -17,6 +17,7 @@ See docs/cache.txt for information on the public API.
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)
+
diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py
index e25d7a10fb..fa0de8dab9 100644
--- a/django/core/cache/backends/memcached.py
+++ b/django/core/cache/backends/memcached.py
@@ -39,3 +39,7 @@ class CacheClass(BaseCache):
def get_many(self, keys):
return self._cache.get_multi(map(smart_str,keys))
+
+ def close(self, **kwargs):
+ self._cache.disconnect_all()
+