1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #28601 -- Prevented cache.get_or_set() from caching None if default is a callable that returns None.

This commit is contained in:
Daniel Tao
2017-09-15 16:16:44 -05:00
committed by Tim Graham
parent 0e212a705e
commit 4d60261b2a
3 changed files with 14 additions and 6 deletions

View File

@@ -155,13 +155,15 @@ class BaseCache:
Return the value of the key stored or retrieved.
"""
val = self.get(key, version=version)
if val is None and default is not None:
if val is None:
if callable(default):
default = default()
self.add(key, default, timeout=timeout, version=version)
# Fetch the value again to avoid a race condition if another caller
# added a value between the first get() and the add() above.
return self.get(key, default, version=version)
if default is not None:
self.add(key, default, timeout=timeout, version=version)
# Fetch the value again to avoid a race condition if another
# caller added a value between the first get() and the add()
# above.
return self.get(key, default, version=version)
return val
def has_key(self, key, version=None):