From 8e838d9c869083597dc9e161ae2fe37acaa90de9 Mon Sep 17 00:00:00 2001 From: Emre Yilmaz Date: Wed, 2 Dec 2015 12:34:31 +0200 Subject: [PATCH] Fixed #25840 -- Fixed BaseCache.get_or_set() on the DummyCache backend. This also fixes a possible data eviction race condition between setting and getting a key. Another thread could remove the key before get_and_set() accesses it again. In this case, now the default value will be returned instead of None. --- django/core/cache/backends/base.py | 2 +- docs/releases/1.9.1.txt | 3 ++- tests/cache/tests.py | 9 +++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py index 186a45ddd68..af262fecdd1 100644 --- a/django/core/cache/backends/base.py +++ b/django/core/cache/backends/base.py @@ -165,7 +165,7 @@ class BaseCache(object): default = default() val = self.add(key, default, timeout=timeout, version=version) if val: - return self.get(key, version=version) + return self.get(key, default, version) return val def has_key(self, key, version=None): diff --git a/docs/releases/1.9.1.txt b/docs/releases/1.9.1.txt index cdf7bb35caf..91a0f33a2ad 100644 --- a/docs/releases/1.9.1.txt +++ b/docs/releases/1.9.1.txt @@ -9,4 +9,5 @@ Django 1.9.1 fixes several bugs in 1.9. Bugfixes ======== -* ... +* Fixed ``BaseCache.get_or_set()`` with the ``DummyCache`` backend + (:ticket:`25840`). diff --git a/tests/cache/tests.py b/tests/cache/tests.py index ba3745847b9..cd61690d34d 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -202,6 +202,15 @@ class DummyCacheTests(SimpleTestCase): self.assertRaises(ValueError, cache.decr_version, 'answer') self.assertRaises(ValueError, cache.decr_version, 'does_not_exist') + def test_get_or_set(self): + self.assertEqual(cache.get_or_set('mykey', 'default'), 'default') + + def test_get_or_set_callable(self): + def my_callable(): + return 'default' + + self.assertEqual(cache.get_or_set('mykey', my_callable), 'default') + def custom_key_func(key, key_prefix, version): "A customized cache key function"