Skip to content

Commit 5d5ebb5

Browse files
author
anthonyp97
committed
use sentinels to check for cache key existance
1 parent d4d1b32 commit 5d5ebb5

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

cache_helper/decorators.py

Lines changed: 12 additions & 10 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+
sentinel = object()
2828
try:
2929
# Attempts to get cache key and defaults to a string constant which will be returned if the cache
3030
# 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+
sentinel = object()
8283
try:
8384
# Attempts to get cache key and defaults to a string constant which will be returned if the cache
8485
# 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+
sentinel = object()
157159
try:
158160
# Attempts to get cache key and defaults to a string constant which will be returned if the cache
159161
# 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)