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
def cached (timeout ):
18
20
def _cached (func ):
@@ -24,22 +26,21 @@ def wrapper(*args, **kwargs):
24
26
cache_key = utils .get_hashed_cache_key (function_cache_key )
25
27
26
28
try :
27
- value = cache .get (cache_key )
29
+ value = cache .get (cache_key , CACHE_KEY_NOT_FOUND )
28
30
except Exception :
29
31
logger .warning (
30
32
f'Error retrieving value from Cache for Key: { function_cache_key } ' ,
31
33
exc_info = True ,
32
34
)
33
- value = None
35
+ value = CACHE_KEY_NOT_FOUND
34
36
35
- if value is None :
37
+ if value == CACHE_KEY_NOT_FOUND :
36
38
value = func (* args , ** kwargs )
37
39
# Try and set the key, value pair in the cache.
38
40
# But if it fails on an error from the underlying
39
41
# cache system, handle it.
40
42
try :
41
43
cache .set (cache_key , value , timeout )
42
-
43
44
except CacheSetError :
44
45
logger .warning (
45
46
f'Error saving value to Cache for Key: { function_cache_key } ' ,
@@ -79,15 +80,15 @@ def wrapper(*args, **kwargs):
79
80
cache_key = utils .get_hashed_cache_key (function_cache_key )
80
81
81
82
try :
82
- value = cache .get (cache_key )
83
+ value = cache .get (cache_key , CACHE_KEY_NOT_FOUND )
83
84
except Exception :
84
85
logger .warning (
85
86
f'Error retrieving value from Cache for Key: { function_cache_key } ' ,
86
87
exc_info = True ,
87
88
)
88
- value = None
89
+ value = CACHE_KEY_NOT_FOUND
89
90
90
- if value is None :
91
+ if value == CACHE_KEY_NOT_FOUND :
91
92
value = func (* args , ** kwargs )
92
93
# Try and set the key, value pair in the cache.
93
94
# But if it fails on an error from the underlying
@@ -151,14 +152,15 @@ def __get__(self, obj, objtype):
151
152
def __call__ (self , * args , ** kwargs ):
152
153
cache_key , function_cache_key = self .create_cache_key (* args , ** kwargs )
153
154
try :
154
- value = cache .get (cache_key )
155
+ value = cache .get (cache_key , CACHE_KEY_NOT_FOUND )
155
156
except Exception :
156
157
logger .warning (
157
158
f'Error retrieving value from Cache for Key: { function_cache_key } ' ,
158
159
exc_info = True ,
159
160
)
160
- value = None
161
- if value is None :
161
+ value = CACHE_KEY_NOT_FOUND
162
+
163
+ if value == CACHE_KEY_NOT_FOUND :
162
164
value = self .func (* args , ** kwargs )
163
165
# Try and set the key, value pair in the cache.
164
166
# But if it fails on an error from the underlying
@@ -170,6 +172,7 @@ def __call__(self, *args, **kwargs):
170
172
f'Error saving value to Cache for Key: { function_cache_key } ' ,
171
173
exc_info = True ,
172
174
)
175
+
173
176
return value
174
177
175
178
def _invalidate (self , * args , ** kwargs ):
0 commit comments