diff --git a/model_helpers.py b/model_helpers.py index 9818325..3157792 100644 --- a/model_helpers.py +++ b/model_helpers.py @@ -145,7 +145,7 @@ def get_x(obj): # If not cached, call the actual method and cache the result if result is None: result = f(obj) - cache.set(cache_key, result, cache_timeout) + set_x(obj, result) return result def del_x(obj): @@ -166,7 +166,10 @@ def set_x(obj, value): """ cache_key = _get_cache_key(obj) # Save that key in the cache - cache.set(cache_key, value, cache_timeout) + if cache_timeout is None: + cache.set(cache_key, value) + else: + cache.set(cache_key, value, cache_timeout) if readonly: return property(fget=get_x, fdel=del_x) diff --git a/tests/simple_app/simple_app/settings.py b/tests/simple_app/simple_app/settings.py index 310d732..7d59a4c 100644 --- a/tests/simple_app/simple_app/settings.py +++ b/tests/simple_app/simple_app/settings.py @@ -84,7 +84,7 @@ 'default': { 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', 'LOCATION': 'cache', - 'TIMEOUT': 1200 # Every 20 Minute + 'TIMEOUT': 3 # Every 3 seconds } } diff --git a/tests/test_cached_property.py b/tests/test_cached_property.py index 14c4274..23e6186 100644 --- a/tests/test_cached_property.py +++ b/tests/test_cached_property.py @@ -62,3 +62,11 @@ def test_cache_timeout(): sleep(2) tools.assert_equal(team.one_sec_cache, 2) tools.assert_equal(team.one_sec_cache, 2) + + # test default cache timeout (we set it to 3 seconds) + team = Team(name="Team1") + del team.cached_counter + tools.assert_equal(team.cached_counter, 1) + tools.assert_equal(team.cached_counter, 1) + sleep(4) + tools.assert_equal(team.cached_counter, 2)