diff --git a/expiringdict/__init__.py b/expiringdict/__init__.py index 90d086b..df8ec8c 100755 --- a/expiringdict/__init__.py +++ b/expiringdict/__init__.py @@ -60,7 +60,7 @@ def __contains__(self, key): try: with self.lock: item = OrderedDict.__getitem__(self, key) - if time.time() - item[1] < self.max_age: + if self.max_age == -1 or time.time() - item[1] < self.max_age: return True else: del self[key] @@ -76,7 +76,7 @@ def __getitem__(self, key, with_age=False): with self.lock: item = OrderedDict.__getitem__(self, key) item_age = time.time() - item[1] - if item_age < self.max_age: + if self.max_age == -1 or item_age < self.max_age: if with_age: return item[0], item_age else: @@ -204,7 +204,7 @@ def __assert_max_len(max_len): @staticmethod def __assert_max_age_seconds(max_age_seconds): - assert max_age_seconds >= 0 + assert max_age_seconds >= -1 @staticmethod def __is_reduced_result(items): diff --git a/tests/expiringdict_test.py b/tests/expiringdict_test.py index df2fd8b..fcbdab1 100644 --- a/tests/expiringdict_test.py +++ b/tests/expiringdict_test.py @@ -133,3 +133,9 @@ def test_reset_of_key_no_trim(): d["b"] = "B" assert "a" in d + +def test_non_expiring(): + d = ExpiringDict(max_len=1, max_age_seconds=-1) + d['a'] = 'x' + sleep(1) + eq_(d.get('a'), 'x')