Refs #29887 -- Simplified memcached client instantiation.
This commit is contained in:
parent
7a60670b78
commit
cc1f2c6a19
|
@ -23,17 +23,15 @@ class BaseMemcachedCache(BaseCache):
|
||||||
self.LibraryValueNotFoundException = value_not_found_exception
|
self.LibraryValueNotFoundException = value_not_found_exception
|
||||||
|
|
||||||
self._lib = library
|
self._lib = library
|
||||||
|
self._class = library.Client
|
||||||
self._options = params.get('OPTIONS') or {}
|
self._options = params.get('OPTIONS') or {}
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def _cache(self):
|
def _cache(self):
|
||||||
"""
|
"""
|
||||||
Implement transparent thread-safe access to a memcached client.
|
Implement transparent thread-safe access to a memcached client.
|
||||||
"""
|
"""
|
||||||
if getattr(self, '_client', None) is None:
|
return self._class(self._servers, **self._options)
|
||||||
self._client = self._lib.Client(self._servers, **self._options)
|
|
||||||
|
|
||||||
return self._client
|
|
||||||
|
|
||||||
def get_backend_timeout(self, timeout=DEFAULT_TIMEOUT):
|
def get_backend_timeout(self, timeout=DEFAULT_TIMEOUT):
|
||||||
"""
|
"""
|
||||||
|
@ -166,14 +164,7 @@ class MemcachedCache(BaseMemcachedCache):
|
||||||
# incr/decr(), python-memcached < 1.45 raises ValueError.
|
# incr/decr(), python-memcached < 1.45 raises ValueError.
|
||||||
import memcache
|
import memcache
|
||||||
super().__init__(server, params, library=memcache, value_not_found_exception=ValueError)
|
super().__init__(server, params, library=memcache, value_not_found_exception=ValueError)
|
||||||
|
self._options = {'pickleProtocol': pickle.HIGHEST_PROTOCOL, **self._options}
|
||||||
@property
|
|
||||||
def _cache(self):
|
|
||||||
if getattr(self, '_client', None) is None:
|
|
||||||
client_kwargs = {'pickleProtocol': pickle.HIGHEST_PROTOCOL}
|
|
||||||
client_kwargs.update(self._options)
|
|
||||||
self._client = self._lib.Client(self._servers, **client_kwargs)
|
|
||||||
return self._client
|
|
||||||
|
|
||||||
def get(self, key, default=None, version=None):
|
def get(self, key, default=None, version=None):
|
||||||
key = self.make_key(key, version=version)
|
key = self.make_key(key, version=version)
|
||||||
|
@ -201,10 +192,6 @@ class PyLibMCCache(BaseMemcachedCache):
|
||||||
import pylibmc
|
import pylibmc
|
||||||
super().__init__(server, params, library=pylibmc, value_not_found_exception=pylibmc.NotFound)
|
super().__init__(server, params, library=pylibmc, value_not_found_exception=pylibmc.NotFound)
|
||||||
|
|
||||||
@cached_property
|
|
||||||
def _cache(self):
|
|
||||||
return self._lib.Client(self._servers, **self._options)
|
|
||||||
|
|
||||||
def touch(self, key, timeout=DEFAULT_TIMEOUT, version=None):
|
def touch(self, key, timeout=DEFAULT_TIMEOUT, version=None):
|
||||||
key = self.make_key(key, version=version)
|
key = self.make_key(key, version=version)
|
||||||
self.validate_key(key)
|
self.validate_key(key)
|
||||||
|
|
|
@ -1374,7 +1374,7 @@ class BaseMemcachedTests(BaseCacheTests):
|
||||||
# connection is closed when the request is complete.
|
# connection is closed when the request is complete.
|
||||||
signals.request_finished.disconnect(close_old_connections)
|
signals.request_finished.disconnect(close_old_connections)
|
||||||
try:
|
try:
|
||||||
with mock.patch.object(cache._lib.Client, 'disconnect_all', autospec=True) as mock_disconnect:
|
with mock.patch.object(cache._class, 'disconnect_all', autospec=True) as mock_disconnect:
|
||||||
signals.request_finished.send(self.__class__)
|
signals.request_finished.send(self.__class__)
|
||||||
self.assertIs(mock_disconnect.called, self.should_disconnect_on_close)
|
self.assertIs(mock_disconnect.called, self.should_disconnect_on_close)
|
||||||
finally:
|
finally:
|
||||||
|
@ -1383,7 +1383,7 @@ class BaseMemcachedTests(BaseCacheTests):
|
||||||
def test_set_many_returns_failing_keys(self):
|
def test_set_many_returns_failing_keys(self):
|
||||||
def fail_set_multi(mapping, *args, **kwargs):
|
def fail_set_multi(mapping, *args, **kwargs):
|
||||||
return mapping.keys()
|
return mapping.keys()
|
||||||
with mock.patch('%s.Client.set_multi' % self.client_library_name, side_effect=fail_set_multi):
|
with mock.patch.object(cache._class, 'set_multi', side_effect=fail_set_multi):
|
||||||
failing_keys = cache.set_many({'key': 'value'})
|
failing_keys = cache.set_many({'key': 'value'})
|
||||||
self.assertEqual(failing_keys, ['key'])
|
self.assertEqual(failing_keys, ['key'])
|
||||||
|
|
||||||
|
@ -1395,7 +1395,6 @@ class BaseMemcachedTests(BaseCacheTests):
|
||||||
))
|
))
|
||||||
class MemcachedCacheTests(BaseMemcachedTests, TestCase):
|
class MemcachedCacheTests(BaseMemcachedTests, TestCase):
|
||||||
base_params = MemcachedCache_params
|
base_params = MemcachedCache_params
|
||||||
client_library_name = 'memcache'
|
|
||||||
|
|
||||||
def test_memcached_uses_highest_pickle_version(self):
|
def test_memcached_uses_highest_pickle_version(self):
|
||||||
# Regression test for #19810
|
# Regression test for #19810
|
||||||
|
@ -1427,7 +1426,6 @@ class MemcachedCacheTests(BaseMemcachedTests, TestCase):
|
||||||
))
|
))
|
||||||
class PyLibMCCacheTests(BaseMemcachedTests, TestCase):
|
class PyLibMCCacheTests(BaseMemcachedTests, TestCase):
|
||||||
base_params = PyLibMCCache_params
|
base_params = PyLibMCCache_params
|
||||||
client_library_name = 'pylibmc'
|
|
||||||
# libmemcached manages its own connections.
|
# libmemcached manages its own connections.
|
||||||
should_disconnect_on_close = False
|
should_disconnect_on_close = False
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue