Open meduzen opened 1 year ago
The usage could look like this, according to first (rather successful) experiment:
let cat = cache.rememberDuring('cat', 'Drich', 2000) // 'Drich'
cat = cache.rememberDuring('cat', 'Kisa', 200) // still 'Drich'
await setTimeout(200) // wait…
cat = cache.rememberDuring('cat', 'Pitch', 200) // 'Pitch', because previous entry has expired
For this, the place for metadata is another Map
object. We can’t use WeakMap
because it doesn’t accept strings as key. Each metadata entry should be an object containing an expiration Date
.
Prospective work:
Date
object is passed instead of an integer, the Date
object becomes the expiration. But then the method shouldn’t be named rememberDuring
.shouldUpdateTheCache
condition” (when truthy). In that case, remember
and add
with a 3rd parameter accepting either an integer (duration), either a Date (expiration moment) This is at the time of writing all done in #10.
At first sight, it will probably be more convenient to override most CacheMap
inherited methods, in order to have control of what happens on item retrieval/modification. Gonna experiment this at some point.
One approach for both this and #6 could be to maintain a
WeakMap
Map
dedicated to items metadata.A
Weakmap
Map
item could look like this:valuekey of aCacheMap
item for which we want to have metadata (like the time to live);This way, if I'm correct, when an item is removed from theWe can’t use aCacheMap
, it is automatically garbage collected from theWeakMap
.WeakMap
for this. See next comment.