remembered
v0.11.0
Published
Easy memory cache and semaphore
Downloads
1,134
Readme
A module to remember for a given time the promises you made.
How to install
npm install remembered
Usage
Create a new Remembered instance giving the ttl you want, in ms.
const remembered = new Remembered({ ttl: 1000 });
Now, just call the get method informing a remembering key and a callback:
const callback = () => new Promise<number>((resolve) => {
setTimeout(200, () => resolve(Date.now()));
});
const [r1, r2, r3] = await Promise.all([
remembered.get('test', callback),
remembered.get('test', callback),
remembered.get('test', callback),
]);
In the above example, r1, r2 and r3 will receive the same exact +promise. Remembered don't "cache" the result of your async operation: it caches the promise itself.
This is very useful for concurrent tasks where you have the same heavy call and you want it to happen just once. In this example, the promise is resolved in 200 milliseconds, but the ttl is 1 second and it starts to count not after the promise is resolved, but when the promise is made. In other words, exactly 1 second after the first call, the callback will need to be called again.
If you want for the promise to be remembered just while it is not resolved, you can use ttl 0. In this case, while the promise is pending, Remembered will return the same reference, but, after it is resolved, then callback will be called
Another option is to use the wrap method:
const callback = () => new Promise<number>((resolve) => {
setTimeout(200, () => resolve(Date.now()));
});
const wrapped = remembered.wrap(callback, () => 'test');
const [r1, r2, r3] = await Promise.all([
wrapped(),
wrapped(),
wrapped(),
]);
The wrap method returns a version of your function that receives the exact same arguments, but uses remembered under the hood. The second parameters you inform also receives the same parameters that your first callback receives, but it must return the remembering key.
Important!
The given ttl is meant to be readonly. So, if you change the ttl value of the provided, it will not take effect on the previous Remembered instances.
Saudade
There is no proper translation for the word saudade in English. Saudade is the feeling that you feel when you miss someone. It's the emptiness that is left when someone important to you passes away. This module is a tribute for my dear uncles who died due to complications of COVID-19. Uncles, you'll always be remembered.
- ✞ 2020-04-01: Genivaldo Rodrigues dos Santos
- ✞ 2020-03-21: João Carlos Rodrigues dos Santos
License
Licensed under MIT.