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

Getting the length of the dictionary should return # of items not expired #39

Open
chanchiem opened this issue Aug 1, 2019 · 3 comments

Comments

@chanchiem
Copy link

Since items are only removed on retrieval, calling the len method on the dictionary will return all the items in the dictionary--including the expired items.

Length should iterate through the internal list and find what items are expired and remove them appropriately. Since it's internally implemented using an OrderedDict, we can perform a binary search to find what items in the dictionary have expired in Log(n) time

@chanchiem
Copy link
Author

chanchiem commented Aug 2, 2019

PR #40 fixes it. Doesn't use binary search approach yet. setitem cleans up implicitly, so amortized, it should be fast enough. If it makes a big performance impact, we know what algorithm would work best.

@lucascott
Copy link

I've noticed the bug still persists. The len() function applied to the ExpiringDict object doesn't return the expected value unless the same function is applied to the list of values first.

Code to reproduce the bug:

from expiringdict import ExpiringDict
import time
ed = ExpiringDict(max_len=8, max_age_seconds=2)
ed['a'] = 5
time.sleep(3)
print(len(ed))
print(len(ed.values()))
print(len(ed))

Output:

1
0
0

@vshah-lifesize
Copy link

@lucascott - Thats how its supposed to work, afaik. Expiring items are removed when the next mutating operation happens, so for a while they'll still claim memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants