Skip to content

Commit

Permalink
None result obtained from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
poleha committed Oct 12, 2018
1 parent 4674dfd commit fda8f8b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/cache_memoize/__init__.py
@@ -1,10 +1,11 @@
import hashlib
from functools import wraps

import hashlib
from django.core.cache import caches, DEFAULT_CACHE_ALIAS

from django.utils.encoding import force_text, force_bytes

NONE_PLACEHOLDER = '___NONE_PLACEHOLDER___'


def cache_memoize(
timeout,
Expand Down Expand Up @@ -81,7 +82,7 @@ def callmeonce(arg1):
def noop(*args):
return args
args_rewrite = noop

cache = caches[cache_alias]

def decorator(func):
Expand Down Expand Up @@ -112,10 +113,14 @@ def inner(*args, **kwargs):
cache.set(cache_key, True, timeout)
elif result is not None:
cache.set(cache_key, result, timeout)
else:
cache.set(cache_key, NONE_PLACEHOLDER, timeout)
if miss_callable:
miss_callable(*args, **kwargs)
elif hit_callable:
hit_callable(*args, **kwargs)
if result == NONE_PLACEHOLDER:
result = None
return result

def invalidate(*args, **kwargs):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_cache_memoize.py
Expand Up @@ -202,3 +202,19 @@ def runmeonce(a):
assert len(calls_made) == 2
runmeonce_locmem(10)
assert len(calls_made) == 2


def test_cache_memoize_empty_placeholder():
calls_made = []

@cache_memoize(10)
def runmeonce(a):
calls_made.append(a)

result = runmeonce(20)
assert len(calls_made) == 1
assert result is None
result = runmeonce(20)
runmeonce(20)
assert len(calls_made) == 1
assert result is None

0 comments on commit fda8f8b

Please sign in to comment.