From 0104b5a41704430aaa1067da2281a86a83c8543a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Szafra=C5=84ski?= Date: Fri, 15 Feb 2019 01:57:38 +0100 Subject: [PATCH] Fixed #30181 -- Made cache.get() with default work correctly on PyLibMCCache if None is cached. --- django/core/cache/backends/memcached.py | 15 +++++++++++---- tests/cache/tests.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py index bb27cb53bb..48cfb8310b 100644 --- a/django/core/cache/backends/memcached.py +++ b/django/core/cache/backends/memcached.py @@ -68,10 +68,7 @@ class BaseMemcachedCache(BaseCache): def get(self, key, default=None, version=None): key = self.make_key(key, version=version) - val = self._cache.get(key) - if val is None: - return default - return val + return self._cache.get(key, default) def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None): key = self.make_key(key, version=version) @@ -163,6 +160,16 @@ class MemcachedCache(BaseMemcachedCache): key = self.make_key(key, version=version) return self._cache.touch(key, self.get_backend_timeout(timeout)) != 0 + def get(self, key, default=None, version=None): + key = self.make_key(key, version=version) + val = self._cache.get(key) + # python-memcached doesn't support default values in get(). + # https://github.com/linsomniac/python-memcached/issues/159 + # Remove this method if that issue is fixed. + if val is None: + return default + return val + class PyLibMCCache(BaseMemcachedCache): "An implementation of a cache binding using pylibmc" diff --git a/tests/cache/tests.py b/tests/cache/tests.py index 9f717cfa23..ced86fc8f6 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -274,6 +274,11 @@ class BaseCacheTests: cache.set("key", "value") self.assertEqual(cache.get("key"), "value") + def test_default_used_when_none_is_set(self): + """If None is cached, get() returns it instead of the default.""" + cache.set('key_default_none', None) + self.assertIsNone(cache.get('key_default_none', default='default')) + def test_add(self): # A key can be added to a cache cache.add("addkey1", "value") @@ -1365,6 +1370,14 @@ class MemcachedCacheTests(BaseMemcachedTests, TestCase): def test_memcached_options(self): self.assertEqual(cache._cache.server_max_value_length, 9999) + def test_default_used_when_none_is_set(self): + """ + python-memcached doesn't support default in get() so this test + overrides the one in BaseCacheTests. + """ + cache.set('key_default_none', None) + self.assertEqual(cache.get('key_default_none', default='default'), 'default') + @unittest.skipUnless(PyLibMCCache_params, "PyLibMCCache backend not configured") @override_settings(CACHES=caches_setting_for_tests(