Kaydet (Commit) 8e838d9c authored tarafından Emre Yilmaz's avatar Emre Yilmaz Kaydeden (comit) Tim Graham

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.
üst 25f5b5c1
...@@ -165,7 +165,7 @@ class BaseCache(object): ...@@ -165,7 +165,7 @@ class BaseCache(object):
default = default() default = default()
val = self.add(key, default, timeout=timeout, version=version) val = self.add(key, default, timeout=timeout, version=version)
if val: if val:
return self.get(key, version=version) return self.get(key, default, version)
return val return val
def has_key(self, key, version=None): def has_key(self, key, version=None):
......
...@@ -9,4 +9,5 @@ Django 1.9.1 fixes several bugs in 1.9. ...@@ -9,4 +9,5 @@ Django 1.9.1 fixes several bugs in 1.9.
Bugfixes Bugfixes
======== ========
* ... * Fixed ``BaseCache.get_or_set()`` with the ``DummyCache`` backend
(:ticket:`25840`).
...@@ -202,6 +202,15 @@ class DummyCacheTests(SimpleTestCase): ...@@ -202,6 +202,15 @@ class DummyCacheTests(SimpleTestCase):
self.assertRaises(ValueError, cache.decr_version, 'answer') self.assertRaises(ValueError, cache.decr_version, 'answer')
self.assertRaises(ValueError, cache.decr_version, 'does_not_exist') 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): def custom_key_func(key, key_prefix, version):
"A customized cache key function" "A customized cache key function"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment