unstorage icon indicating copy to clipboard operation
unstorage copied to clipboard

feat: add `meta` and `ttl` to `memory` driver (new driver)

Open sandros94 opened this issue 1 year ago • 2 comments

Describe the feature

I've been digging into memory driver related issues and toolset (notably: #13, #168, #262).

What are the plans to add TTL and Meta to this driver? I cannot find a clear No, there is no plan because of XYZ. The only mention of in-mem TTL support is a comment suggesting using lru-cache, but its documentation states multiple times that it is not its primary use, and should be avoided if it is the only use (doc source):

If you truly wish to use a cache that is bound only by TTL expiration, consider using a Map object, and calling setTimeout to delete entries when they expire. It will perform much better than an LRU cache.

This made me think that by changing the current Map from Map<string, any> to Map<string, { data: any, meta: { ttl?: number | undefined, [key: string]: any } and a setTimeout with an optional driver option like ttlAutoPurge we could both add ttl and meta support without breaking changes at the small cost of memory increase and pulling the whole value just to access the metadata.

Another approach is to have a dedicated Map only for metadata, but generally I prefer pulling +10MB objects compared to risking the two Maps go out of sync (but, tbh, idk if I'm being paranoid)

Additional information

  • [x] Would you be willing to help implement this feature?

sandros94 avatar Jan 27 '25 14:01 sandros94

Also, worth mentioning since it is mentioned in lru-cache:

For use-cases with the primary focus of using TTL but keep lru-cache's API it is suggested to use @isaacs/ttlcache.

But as I understand it it only supports a global TTL option and not per-key.

sandros94 avatar Jan 27 '25 14:01 sandros94

I ended up creating a new driver, since this impacted both performance and memory allocation compared to the default memory driver. Making all this an opt-in feature rather than a forced one.

Check out linked PR for more info

sandros94 avatar Feb 02 '25 15:02 sandros94