From: George Sakkis Date: Sun, 19 Apr 2015 19:38:14 +0000 (+0300) Subject: Allow del statement to work for cached properties with ttl X-Git-Tag: 1.2.0~5^2~7 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=6dfcdece430112596b7528a311d316b9723abac0;p=cached-property.git Allow del statement to work for cached properties with ttl --- diff --git a/cached_property.py b/cached_property.py index 2d5d50c..25ffe47 100644 --- a/cached_property.py +++ b/cached_property.py @@ -94,6 +94,12 @@ class cached_property_with_ttl(object): return value + def __delete__(self, obj): + try: + del obj._cache[self.__name__] + except (KeyError, AttributeError): + pass + # Aliases to make cached_property_with_ttl easier to use cached_property_ttl = cached_property_with_ttl timed_cached_property = cached_property_with_ttl diff --git a/tests/test_cached_property_ttl.py b/tests/test_cached_property_ttl.py index 5802516..343ca2e 100644 --- a/tests/test_cached_property_ttl.py +++ b/tests/test_cached_property_ttl.py @@ -71,12 +71,15 @@ class TestCachedPropertyWithTTL(unittest.TestCase): c = Check() + # Resetting the cache before it is set is a no-op + del c.add_cached + # Run standard cache assertion self.assertEqual(c.add_cached, 1) self.assertEqual(c.add_cached, 1) # Reset the cache. - del c._cache['add_cached'] + del c.add_cached self.assertEqual(c.add_cached, 2) self.assertEqual(c.add_cached, 2) @@ -205,12 +208,15 @@ class TestThreadedCachedPropertyWithTTL(unittest.TestCase): c = Check() + # Resetting the cache before it is set is a no-op + del c.add_cached + # Run standard cache assertion self.assertEqual(c.add_cached, 1) self.assertEqual(c.add_cached, 1) # Reset the cache. - del c._cache['add_cached'] + del c.add_cached self.assertEqual(c.add_cached, 2) self.assertEqual(c.add_cached, 2)