Self refreshable in-memory data cache
npm install frescasSelf-refreshable in-memory data cache.
- FreshValue and AsyncFreshValue - single refreshable value.
- FreshValueMap and AsyncFreshValueMap - maps of refreshable values.
``typescriptload
/* Create a refreshable value. Define 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:
- FreshValue & unit tests.
- FreshValueMap & unit tests.