tc39 / proposal-policy-map-set

A TC39 proposal for Maps and Sets with cache replacement policies like LRU and LFU.
BSD 3-Clause "New" or "Revised" License
15 stars 2 forks source link

Consider TTL in constructor options / `.set` #13

Open shhac opened 1 year ago

shhac commented 1 year ago

Other than remembering commonly used invocations, another big use case for a cache is knowing when an item is time-stale.

Currently doing this with a Map or Set is possible by wrapping it manually, but having it wrapped outside has drawbacks that inclusion here could tackle

ttl would default to never expires when not provided, and could be provided either at the level of the whole map/set as an option at construction time, or by adding an additional parameter to the .set method

const m = new Foo();
m.set('key', 'value', msToLive);
// or
m.set('key', 'value', { ttl: msToLive });