Skip to content

Commit 3bd4ba8

Browse files
Alex PolehaPeter Bengtsson
authored andcommitted
None result obtained from cache (#9)
1 parent f0f56fd commit 3bd4ba8

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/cache_memoize/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from django.utils.encoding import force_text, force_bytes
77

8+
MARKER = object()
9+
810

911
def cache_memoize(
1012
timeout,
@@ -99,23 +101,22 @@ def _make_cache_key(*args, **kwargs):
99101

100102
@wraps(func)
101103
def inner(*args, **kwargs):
102-
refresh = kwargs.pop('_refresh', False)
103104
if key_generator_callable is None:
104105
cache_key = _make_cache_key(*args, **kwargs)
105106
else:
106107
cache_key = key_generator_callable(*args, **kwargs)
107-
if refresh:
108-
result = None
108+
if kwargs.pop('_refresh', False):
109+
result = MARKER
109110
else:
110-
result = cache.get(cache_key)
111-
if result is None:
111+
result = cache.get(cache_key, MARKER)
112+
if result is MARKER:
112113
result = func(*args, **kwargs)
113114
if not store_result:
114115
# Then the result isn't valuable/important to store but
115116
# we want to store something. Just to remember that
116117
# it has be done.
117118
cache.set(cache_key, True, timeout)
118-
elif result is not None:
119+
else:
119120
cache.set(cache_key, result, timeout)
120121
if miss_callable:
121122
miss_callable(*args, **kwargs)

tests/test_cache_memoize.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,18 @@ def runmeonce(arg1, arg2):
222222
assert len(calls_made) == 1
223223
runmeonce(1, 3)
224224
assert len(calls_made) == 2
225+
226+
227+
def test_cache_memoize_none_value():
228+
calls_made = []
229+
230+
@cache_memoize(10)
231+
def runmeonce(a):
232+
calls_made.append(a)
233+
234+
result = runmeonce(20)
235+
assert len(calls_made) == 1
236+
assert result is None
237+
result = runmeonce(20)
238+
assert len(calls_made) == 1
239+
assert result is None

0 commit comments

Comments
 (0)