diff --git a/expiringdict/__init__.py b/expiringdict/__init__.py index ad27546..b869190 100755 --- a/expiringdict/__init__.py +++ b/expiringdict/__init__.py @@ -58,14 +58,17 @@ def __init__(self, max_len, max_age_seconds, items=None): def __len__(self): with self.lock: current_key = iter(self) + keys_to_del = [] for k in current_key: item = OrderedDict.__getitem__(self, k) time_added = item[1] item_age = time.time() - time_added if item_age > self.max_age: - del self[k] + keys_to_del.append(k) else: break + for k in keys_to_del: + del self[k] return super(ExpiringDict, self).__len__() diff --git a/tests/expiringdict_test.py b/tests/expiringdict_test.py index bf17a39..6663981 100644 --- a/tests/expiringdict_test.py +++ b/tests/expiringdict_test.py @@ -65,7 +65,7 @@ def test_repr(): d['a'] = 'x' eq_(str(d), "ExpiringDict([('a', 'x')])") sleep(0.01) - eq_(str(d), "ExpiringDict()") + eq_(str(d), "ExpiringDict([])") def test_iter():