isaacs / node-lru-cache

A fast cache that automatically deletes the least recently used items
http://isaacs.github.io/node-lru-cache/
ISC License
5.38k stars 353 forks source link

length do not take maxAge into account #165

Closed songpr closed 2 years ago

songpr commented 4 years ago

The length function do not take maxAge into account. I would like to check valid length of item in cache but the length just return the invalid legth.

Below is test case in jest

const LRU = require("lru-cache"); //cache for 1 hours, max 1000 fields //reasons profiles field will use for validate all user profile fields update // and we will not update them often const cache_option = { max: 2000 , maxAge: 2000 }; //maxAge: 1000 60 60 * 24 const cache = new LRU(cache_option); test("", async(done) => { cache.set("1", 1); expect(cache.length).toBe(1); setTimeout(()=>{ expect(cache.length).toBe(0); done(); },4000); })

jeyendranbalakrishnan commented 2 years ago

I know this is old, but this is expected behavior. The documentation clearly says maxAge does not preemptievely delete items from cache, but is only invoked when there is a get or set.

isaacs commented 2 years ago

Yep. Working as intended. maxAge is advisory, we don't set timers and delete things proactively.