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

itemCount is not reduced when trim() is executed #13

Closed
snatesan opened this issue Dec 18, 2012 · 3 comments
Closed

itemCount is not reduced when trim() is executed #13

snatesan opened this issue Dec 18, 2012 · 3 comments

Comments

@snatesan
Copy link

When trim() is executed to remove the items when length > max then itemCount is not reduced. Because of that cache.keys().length becomes more than max.

For example
var LRU = require("lru-cache");
var options = { max: 5 };
var cache = LRU(options);
cache.set("key1", "value1");
cache.set("key2", "value2");
cache.set("key3", "value3");
cache.set("key4", "value4");
cache.set("key5", "value5");
cache.set("key6", "value6");

cache.keys() now returns [ 'key6',
'key5',
'key4',
'key3',
'key2',
] and cache.keys().length returns 6

when you do add one more value to the cache like cache.set("key7", "value7"); then
cache.keys() returns
[ 'key7',
'key6',
'key5',
'key4',
'key3',
,
] and cache.keys().length returns 7.

I wonder whether this will lead to a memory leak when we add more items to the cache even if there is "max" property is set.

@isaacs
Copy link
Owner

isaacs commented Dec 30, 2012

Yes, this is a bug. I keep meaning to fix it, but getting distracted by other things. Wanna send a pull req? Should be pretty easy.

@snatesan
Copy link
Author

snatesan commented Jan 2, 2013

Yes, will do.

@isaacs
Copy link
Owner

isaacs commented Jan 15, 2013

Fixed by b02186e.

@isaacs isaacs closed this as completed Jan 15, 2013
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

2 participants