npm install --save @mish-tv/cache
import { Cache } from "@mish-tv/cache";
// By default, it caches for 1hour + rand()*6min.
const entityCache = new Cache<Entity>();
// Only at the beginning and when it expires,
// it calls an anonymous function passed as an argument and
// caches the returned value.
const getEntity = () =>
entityCache.get(async () => {
const entity: Entity = await fetch();
return entity;
});
(async () => {
const entity = await getEntity();
})();
// You can configure ttl, jitter and initial value.
const entityCache = new Cache(60000, 1000, entity);
// You can create a cache that will never expire.
const entityCache = new Cache("infinity");