Skip to content

Commit 9c09d39

Browse files
Merge pull request #22 from anthonyp97/add_logging_none_cache_value
2 parents 1da2963 + a15910e commit 9c09d39

File tree

3 files changed

+31
-79
lines changed

3 files changed

+31
-79
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, 6)
1+
VERSION = (1, 0, 7)

cache_helper/decorators.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ def wrapper(*args, **kwargs):
3636
)
3737
value = sentinel
3838

39-
if value is sentinel:
39+
# If there is an issue with our cache client deserializing the value (due to memory or some other issue),
40+
# we get a None response so log anytime this happens
41+
if value is None:
42+
logger.warning(
43+
'None cache value found for cache key: {}, function cache key: {}, value: {}'.format(
44+
cache_key, function_cache_key, value
45+
)
46+
)
47+
48+
if value is sentinel or value is None:
4049
value = func(*args, **kwargs)
4150
# Try and set the key, value pair in the cache.
4251
# But if it fails on an error from the underlying
@@ -91,7 +100,16 @@ def wrapper(*args, **kwargs):
91100
)
92101
value = sentinel
93102

94-
if value is sentinel:
103+
# If there is an issue with our cache client deserializing the value (due to memory or some other issue),
104+
# we get a None response so log anytime this happens
105+
if value is None:
106+
logger.warning(
107+
'None cache value found for cache key: {}, function cache key: {}, value: {}'.format(
108+
cache_key, function_cache_key, value
109+
)
110+
)
111+
112+
if value is sentinel or value is None:
95113
value = func(*args, **kwargs)
96114
# Try and set the key, value pair in the cache.
97115
# But if it fails on an error from the underlying
@@ -167,7 +185,16 @@ def __call__(self, *args, **kwargs):
167185
)
168186
value = sentinel
169187

170-
if value is sentinel:
188+
# If there is an issue with our cache client deserializing the value (due to memory or some other issue),
189+
# we get a None response so log anytime this happens
190+
if value is None:
191+
logger.warning(
192+
'None cache value found for cache key: {}, function cache key: {}, value: {}'.format(
193+
cache_key, function_cache_key, value
194+
)
195+
)
196+
197+
if value is sentinel or value is None:
171198
value = self.func(*args, **kwargs)
172199
# Try and set the key, value pair in the cache.
173200
# But if it fails on an error from the underlying

test_project/test_project/tests.py

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,6 @@ def func_with_multiple_args_and_kwargs(
4141
):
4242
return datetime.utcnow()
4343

44-
@cached_instance_method(60 * 60)
45-
def instance_increment_by_none_return(self, num):
46-
self.instance_counter += num
47-
return None
48-
49-
@classmethod
50-
@cached_class_method(60 * 60)
51-
def class_increment_by_none_return(cls, num):
52-
cls.class_counter += num
53-
return None
54-
55-
@staticmethod
56-
@cached(60 * 60)
57-
def static_increment_by_none_return(num):
58-
global GLOBAL_COUNTER
59-
GLOBAL_COUNTER += num
60-
61-
return None
62-
6344

6445
class SubclassIncrementer(Incrementer):
6546
class_counter = 500
@@ -289,26 +270,6 @@ def test_invalidate_instance_method(self):
289270
# but 2 is still stale
290271
self.assertEqual(incrementer.instance_increment_by(2), 103)
291272

292-
def test_cached_instance_method_none_return(self):
293-
"""
294-
Tests that calling a cached instance method returning None still uses the cache as expected.
295-
"""
296-
incrementer = Incrementer(100)
297-
298-
# Hasn't been computed before, so the function actually gets called
299-
incrementer.instance_increment_by_none_return(1)
300-
self.assertEqual(incrementer.instance_counter, 101)
301-
302-
incrementer.instance_increment_by_none_return(2)
303-
self.assertEqual(incrementer.instance_counter, 103)
304-
305-
# We expect the instance counter to stay the same as the last time it was updated and not increment again
306-
incrementer.instance_increment_by_none_return(1)
307-
self.assertEqual(incrementer.instance_counter, 103)
308-
309-
incrementer.instance_increment_by_none_return(2)
310-
self.assertEqual(incrementer.instance_counter, 103)
311-
312273

313274
class CachedClassMethodTests(TestCase):
314275
def tearDown(self):
@@ -451,24 +412,6 @@ def test_invalidate_class_method(self):
451412
# but 2 is still stale
452413
self.assertEqual(Incrementer.class_increment_by(2), 503)
453414

454-
def test_cached_class_method_none_return(self):
455-
"""
456-
Tests that calling a cached class method returning None still uses the cache as expected.
457-
"""
458-
# Hasn't been computed before, so the function actually gets called
459-
Incrementer.class_increment_by_none_return(1)
460-
self.assertEqual(Incrementer.class_counter, 501)
461-
462-
Incrementer.class_increment_by_none_return(2)
463-
self.assertEqual(Incrementer.class_counter, 503)
464-
465-
# We expect the class counter to stay the same as the last time it was updated and not increment again
466-
Incrementer.class_increment_by_none_return(1)
467-
self.assertEqual(Incrementer.class_counter, 503)
468-
469-
Incrementer.class_increment_by_none_return(2)
470-
self.assertEqual(Incrementer.class_counter, 503)
471-
472415

473416
class CachedStaticMethodTests(TestCase):
474417
def tearDown(self):
@@ -611,24 +554,6 @@ def test_invalidate_static_method(self):
611554
# but 2 is still stale
612555
self.assertEqual(Incrementer.get_datetime(2), initial_datetime_2)
613556

614-
def test_cached_class_method_none_return(self):
615-
"""
616-
Tests that calling a cached static method returning None still uses the cache as expected.
617-
"""
618-
# Hasn't been computed before, so the function actually gets called
619-
Incrementer.static_increment_by_none_return(1)
620-
self.assertEqual(GLOBAL_COUNTER, 201)
621-
622-
Incrementer.static_increment_by_none_return(2)
623-
self.assertEqual(GLOBAL_COUNTER, 203)
624-
625-
# We expect the global counter to stay the same as the last time it was updated and not increment again
626-
Incrementer.static_increment_by_none_return(1)
627-
self.assertEqual(GLOBAL_COUNTER, 203)
628-
629-
Incrementer.static_increment_by_none_return(2)
630-
self.assertEqual(GLOBAL_COUNTER, 203)
631-
632557

633558
class CacheHelperCacheableTests(TestCase):
634559
def tearDown(self):

0 commit comments

Comments
 (0)