Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add non-expiring cache capability #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions expiringdict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions tests/expiringdict_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')