diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py index 19671b4872..4cf25fb4c6 100644 --- a/django/core/cache/backends/memcached.py +++ b/django/core/cache/backends/memcached.py @@ -103,6 +103,7 @@ class BaseMemcachedCache(BaseCache): return ret def close(self, **kwargs): + # Many clients don't clean up connections properly. self._cache.disconnect_all() def incr(self, key, delta=1, version=None): @@ -202,3 +203,8 @@ class PyLibMCCache(BaseMemcachedCache): @cached_property def _cache(self): return self._lib.Client(self._servers, **self._options) + + def close(self, **kwargs): + # libmemcached manages its own connections. Don't call disconnect_all() + # as it resets the failover state and creates unnecessary reconnects. + pass diff --git a/tests/cache/tests.py b/tests/cache/tests.py index f9190ed55f..b8bce5e721 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -1156,6 +1156,10 @@ memcached_excluded_caches = {'cull', 'zero_cull'} class BaseMemcachedTests(BaseCacheTests): + # By default it's assumed that the client doesn't clean up connections + # properly, in which case the backend must do so after each request. + should_disconnect_on_close = True + def test_location_multiple_servers(self): locations = [ ['server1.tld', 'server2:11211'], @@ -1244,7 +1248,7 @@ class BaseMemcachedTests(BaseCacheTests): # connection is closed when the request is complete. with mock.patch.object(cache._lib.Client, 'disconnect_all', autospec=True) as mock_disconnect: signals.request_finished.send(self.__class__) - self.assertIs(mock_disconnect.called, True) + self.assertIs(mock_disconnect.called, self.should_disconnect_on_close) @unittest.skipUnless(MemcachedCache_params, "MemcachedCache backend not configured") @@ -1276,6 +1280,8 @@ class MemcachedCacheTests(BaseMemcachedTests, TestCase): )) class PyLibMCCacheTests(BaseMemcachedTests, TestCase): base_params = PyLibMCCache_params + # libmemcached manages its own connections. + should_disconnect_on_close = False # By default, pylibmc/libmemcached don't verify keys client-side and so # this test triggers a server-side bug that causes later tests to fail