Skip to content

Commit 1da2963

Browse files
Merge pull request #21 from anthonyp97/fix_value_comparison_check
use sentinels to check for cache key existance
2 parents d4d1b32 + fce1d54 commit 1da2963

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

cache_helper/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = (1, 0, 5)
1+
VERSION = (1, 0, 6)

cache_helper/decorators.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from cache_helper import utils
1414

1515
logger = logging.getLogger(__name__)
16-
CACHE_KEY_NOT_FOUND = 'cache_key_not_found'
1716

1817

1918
def cached(timeout):
@@ -25,18 +24,19 @@ def wrapper(*args, **kwargs):
2524
function_cache_key = utils.get_function_cache_key(func_name, args, kwargs)
2625
cache_key = utils.get_hashed_cache_key(function_cache_key)
2726

27+
# We need to determine whether the object exists in the cache, and since we may have stored a literal value
28+
# None, use a sentinel object as the default
29+
sentinel = object()
2830
try:
29-
# Attempts to get cache key and defaults to a string constant which will be returned if the cache
30-
# key does not exist due to expiry or never being set.
31-
value = cache.get(cache_key, CACHE_KEY_NOT_FOUND)
31+
value = cache.get(cache_key, sentinel)
3232
except Exception:
3333
logger.warning(
3434
f'Error retrieving value from Cache for Key: {function_cache_key}',
3535
exc_info=True,
3636
)
37-
value = CACHE_KEY_NOT_FOUND
37+
value = sentinel
3838

39-
if value == CACHE_KEY_NOT_FOUND:
39+
if value is sentinel:
4040
value = func(*args, **kwargs)
4141
# Try and set the key, value pair in the cache.
4242
# But if it fails on an error from the underlying
@@ -79,18 +79,19 @@ def wrapper(*args, **kwargs):
7979
function_cache_key = utils.get_function_cache_key(func_name, args[1:], kwargs)
8080
cache_key = utils.get_hashed_cache_key(function_cache_key)
8181

82+
# We need to determine whether the object exists in the cache, and since we may have stored a literal value
83+
# None, use a sentinel object as the default
84+
sentinel = object()
8285
try:
83-
# Attempts to get cache key and defaults to a string constant which will be returned if the cache
84-
# key does not exist due to expiry or never being set.
85-
value = cache.get(cache_key, CACHE_KEY_NOT_FOUND)
86+
value = cache.get(cache_key, sentinel)
8687
except Exception:
8788
logger.warning(
8889
f'Error retrieving value from Cache for Key: {function_cache_key}',
8990
exc_info=True,
9091
)
91-
value = CACHE_KEY_NOT_FOUND
92+
value = sentinel
9293

93-
if value == CACHE_KEY_NOT_FOUND:
94+
if value is sentinel:
9495
value = func(*args, **kwargs)
9596
# Try and set the key, value pair in the cache.
9697
# But if it fails on an error from the underlying
@@ -154,18 +155,19 @@ def __get__(self, obj, objtype):
154155
def __call__(self, *args, **kwargs):
155156
cache_key, function_cache_key = self.create_cache_key(*args, **kwargs)
156157

158+
# We need to determine whether the object exists in the cache, and since we may have stored a literal value
159+
# None, use a sentinel object as the default
160+
sentinel = object()
157161
try:
158-
# Attempts to get cache key and defaults to a string constant which will be returned if the cache
159-
# key does not exist due to expiry or never being set.
160-
value = cache.get(cache_key, CACHE_KEY_NOT_FOUND)
162+
value = cache.get(cache_key, sentinel)
161163
except Exception:
162164
logger.warning(
163165
f'Error retrieving value from Cache for Key: {function_cache_key}',
164166
exc_info=True,
165167
)
166-
value = CACHE_KEY_NOT_FOUND
168+
value = sentinel
167169

168-
if value == CACHE_KEY_NOT_FOUND:
170+
if value is sentinel:
169171
value = self.func(*args, **kwargs)
170172
# Try and set the key, value pair in the cache.
171173
# But if it fails on an error from the underlying

0 commit comments

Comments
 (0)