Skip to content

Commit 6855aa0

Browse files
author
anthonyp97
committed
update logic for checking missing / expired cache key
1 parent b6bd5e0 commit 6855aa0

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

cache_helper/decorators.py

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

1515
logger = logging.getLogger(__name__)
16+
CACHE_KEY_NOT_FOUND = 'cache_key_not_found'
17+
1618

1719
def cached(timeout):
1820
def _cached(func):
@@ -24,22 +26,21 @@ def wrapper(*args, **kwargs):
2426
cache_key = utils.get_hashed_cache_key(function_cache_key)
2527

2628
try:
27-
value = cache.get(cache_key)
29+
value = cache.get(cache_key, CACHE_KEY_NOT_FOUND)
2830
except Exception:
2931
logger.warning(
3032
f'Error retrieving value from Cache for Key: {function_cache_key}',
3133
exc_info=True,
3234
)
33-
value = None
35+
value = CACHE_KEY_NOT_FOUND
3436

35-
if value is None:
37+
if value == CACHE_KEY_NOT_FOUND:
3638
value = func(*args, **kwargs)
3739
# Try and set the key, value pair in the cache.
3840
# But if it fails on an error from the underlying
3941
# cache system, handle it.
4042
try:
4143
cache.set(cache_key, value, timeout)
42-
4344
except CacheSetError:
4445
logger.warning(
4546
f'Error saving value to Cache for Key: {function_cache_key}',
@@ -79,15 +80,15 @@ def wrapper(*args, **kwargs):
7980
cache_key = utils.get_hashed_cache_key(function_cache_key)
8081

8182
try:
82-
value = cache.get(cache_key)
83+
value = cache.get(cache_key, CACHE_KEY_NOT_FOUND)
8384
except Exception:
8485
logger.warning(
8586
f'Error retrieving value from Cache for Key: {function_cache_key}',
8687
exc_info=True,
8788
)
88-
value = None
89+
value = CACHE_KEY_NOT_FOUND
8990

90-
if value is None:
91+
if value == CACHE_KEY_NOT_FOUND:
9192
value = func(*args, **kwargs)
9293
# Try and set the key, value pair in the cache.
9394
# But if it fails on an error from the underlying
@@ -151,14 +152,15 @@ def __get__(self, obj, objtype):
151152
def __call__(self, *args, **kwargs):
152153
cache_key, function_cache_key = self.create_cache_key(*args, **kwargs)
153154
try:
154-
value = cache.get(cache_key)
155+
value = cache.get(cache_key, CACHE_KEY_NOT_FOUND)
155156
except Exception:
156157
logger.warning(
157158
f'Error retrieving value from Cache for Key: {function_cache_key}',
158159
exc_info=True,
159160
)
160-
value = None
161-
if value is None:
161+
value = CACHE_KEY_NOT_FOUND
162+
163+
if value == CACHE_KEY_NOT_FOUND:
162164
value = self.func(*args, **kwargs)
163165
# Try and set the key, value pair in the cache.
164166
# But if it fails on an error from the underlying
@@ -170,6 +172,7 @@ def __call__(self, *args, **kwargs):
170172
f'Error saving value to Cache for Key: {function_cache_key}',
171173
exc_info=True,
172174
)
175+
173176
return value
174177

175178
def _invalidate(self, *args, **kwargs):

0 commit comments

Comments
 (0)