rolandstarke / node-cache-manager-fs-hash

Node.js Cache Manager store for filesystem that saves the data in a file named with the hash of the key
MIT License
38 stars 10 forks source link

Feature request: Add `ttl` method #10

Closed ediblecode closed 3 years ago

ediblecode commented 3 years ago

Add a ttl method to support the refreshThreshold property of node-cache-manager for background refreshing. This would essentially allow a stale-while-revalidate pattern (not exactly SWR - it wouldn't revalidate every time) while using node-cache-manager-fs-hash.

For refreshThreshold to work with node-cache-manager it needs "the caching store to provide the ttl method.". I guess this is a similar request to https://github.com/isaacs/node-lru-cache/issues/143 (for a bit more background context).

I think this would be easy enough to implement - the expireTime property stored in each JSON file could easily be used to calculate the ttl of the key. And it could still await the lock to avoid conflicts and support cluster. In fact, it would probably be very similar to the current get method, but would return the ttl rather than data.val.

Happy to have a go at a Pull Request!

rolandstarke commented 3 years ago

Thank you for the input. I added the ttl method and released it under version 1.0.0.

ediblecode commented 3 years ago

@rolandstarke amazing, that was well quick, great work, thank you!