Self-refreshable in-memory data cache.
FreshValue
andAsyncFreshValue
- single refreshable value.FreshValueMap
andAsyncFreshValueMap
- maps of refreshable values.
/** Create a refreshable value. Define `load` function and use correct `refreshPeriodMillis` interval. */
const currentAccessToken = new AsyncFreshValue({
refreshPeriodMillis: 0,
load: async (settings) => {
const response = await fetchAccessToken(/*....*/);
settings.refreshPeriodMillis = Date.now() - response.expirationTime;
return response.token;
}
});
// Use the fresh value :
async function getA(): A {
const accessToken: string = await currentAccessToken.get();
return fetch(accessToken, /** A-call params.*/);
}
async function getB(): B {
const accessToken: string = await currentAccessToken.get();
return fetch(accessToken, /** B-call params.*/);
}
Check in-code documentation and tests: