13
13
from cache_helper import utils
14
14
15
15
logger = logging .getLogger (__name__ )
16
- CACHE_KEY_NOT_FOUND = 'cache_key_not_found'
17
16
18
17
19
18
def cached (timeout ):
@@ -25,18 +24,19 @@ def wrapper(*args, **kwargs):
25
24
function_cache_key = utils .get_function_cache_key (func_name , args , kwargs )
26
25
cache_key = utils .get_hashed_cache_key (function_cache_key )
27
26
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 ()
28
30
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 )
32
32
except Exception :
33
33
logger .warning (
34
34
f'Error retrieving value from Cache for Key: { function_cache_key } ' ,
35
35
exc_info = True ,
36
36
)
37
- value = CACHE_KEY_NOT_FOUND
37
+ value = sentinel
38
38
39
- if value == CACHE_KEY_NOT_FOUND :
39
+ if value is sentinel :
40
40
value = func (* args , ** kwargs )
41
41
# Try and set the key, value pair in the cache.
42
42
# But if it fails on an error from the underlying
@@ -79,18 +79,19 @@ def wrapper(*args, **kwargs):
79
79
function_cache_key = utils .get_function_cache_key (func_name , args [1 :], kwargs )
80
80
cache_key = utils .get_hashed_cache_key (function_cache_key )
81
81
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 ()
82
85
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 )
86
87
except Exception :
87
88
logger .warning (
88
89
f'Error retrieving value from Cache for Key: { function_cache_key } ' ,
89
90
exc_info = True ,
90
91
)
91
- value = CACHE_KEY_NOT_FOUND
92
+ value = sentinel
92
93
93
- if value == CACHE_KEY_NOT_FOUND :
94
+ if value is sentinel :
94
95
value = func (* args , ** kwargs )
95
96
# Try and set the key, value pair in the cache.
96
97
# But if it fails on an error from the underlying
@@ -154,18 +155,19 @@ def __get__(self, obj, objtype):
154
155
def __call__ (self , * args , ** kwargs ):
155
156
cache_key , function_cache_key = self .create_cache_key (* args , ** kwargs )
156
157
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 ()
157
161
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 )
161
163
except Exception :
162
164
logger .warning (
163
165
f'Error retrieving value from Cache for Key: { function_cache_key } ' ,
164
166
exc_info = True ,
165
167
)
166
- value = CACHE_KEY_NOT_FOUND
168
+ value = sentinel
167
169
168
- if value == CACHE_KEY_NOT_FOUND :
170
+ if value is sentinel :
169
171
value = self .func (* args , ** kwargs )
170
172
# Try and set the key, value pair in the cache.
171
173
# But if it fails on an error from the underlying
0 commit comments