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.35k stars 353 forks source link

`ttl` documentation #316

Closed trevor-scheer closed 4 months ago

trevor-scheer commented 1 year ago

Seeking clarification on the ttl option. The README states "positive integer only" but in code it defaults to 0, suggesting 0 is also valid (even if it means the same as undefined. Elsewhere, i.e. the Milliseconds type also states "positive integer only".

Happy to open a PR, just looking for clarity on the expectation/intent before I do (unless you think everything is correct as it stands).

isaacs commented 1 year ago

Yeah, could be documented better.

In practice, 0 means "no TTL", and is the default.

nsunga commented 9 months ago

Via ttl documentation - https://github.com/isaacs/node-lru-cache?tab=readme-ov-file#ttl:

Max time to live for items before they are considered stale.

If 0 is the default value, then does that mean whatever I store in the cache, will the item be considered as stale?

isaacs commented 9 months ago

Yes, zero is the default, which means (on a single item, or the cache as a whole) "do not do track TTL", not "go stale immediately".

It can be any non-negative integer, not just positive, that's a inaccuracy in the docs.

nsunga commented 9 months ago

Awesome! @isaacs One last question - and if you dont get to it, its fine

If my configuration is only { max: 100 }, then would has(key) only be an existence look up? Since ttl would be defaulted to 0

I just want to check if a key is in the cache and with no other side effects

isaacs commented 9 months ago

Yes. has() checks for presence without updating recency. (I think that's maybe configurable iirc?)