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

Feature: Allow length to be passed to set. #160

Closed joeyhub closed 2 years ago

joeyhub commented 4 years ago

Length calculations can sometimes be expensive. For example, if I have something that is already JSON encoded but cache the decoded version then I have to encode it again just to get the length even though I already have the string version to get the length on. Although the length of JSON versus the length of memory is a loose approximation it's close enough for complex data.

A length parameter would go after max age and be used if set.

It is currently possible to my knowledge to set the length to a variable and the length function to take that for the item about to be set, though it's not the most elegant thing.

isaacs commented 2 years ago

In v7, you'll be able to pass a sizeCalculation function as an option to set(), which will override the cache's sizeCalculation method, if specified.

isaacs commented 2 years ago

done in v7