Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
sandros94 opened this issue Jan 27, 2025 · 2 comments · May be fixed by #587
Open
1 task done

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

sandros94 opened this issue Jan 27, 2025 · 2 comments · May be fixed by #587
Labels
enhancement New feature or request

Comments

@sandros94
Copy link

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

  • Would you be willing to help implement this feature?
@sandros94 sandros94 added the enhancement New feature or request label Jan 27, 2025
@sandros94 sandros94 changed the title improve memory driver add meta and ttl to memory driver Jan 27, 2025
@sandros94 sandros94 changed the title add meta and ttl to memory driver feat: add meta and ttl to memory driver Jan 27, 2025
@sandros94
Copy link
Author

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 sandros94 linked a pull request Feb 2, 2025 that will close this issue
3 tasks
@sandros94 sandros94 changed the title feat: add meta and ttl to memory driver feat: add meta and ttl to memory driver (new driver) Feb 2, 2025
@sandros94
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant