const cache1 = new Cache();
cache1.add('foo', 'value');

const cache2 = new Cache();
cache2.match('foo'); // value

const cache3 = new Cache({ namespace: 'foo' });
cache3.match('foo'); // undefined
let COUNTER = 0;
const createPromise = () => new Promise((res) => {
COUNTER++;
setTimeout(() => res('Ok'), 300);
});

const cache = new Cache();
for (let i = 0; i < 10; i++) {
cache.add('foo', createPromise).then((data) => {
console.log(data); // 'Ok'
});
}
console.log(COUNTER); // 1

Type Parameters

  • V = any
  • O extends Record<string, unknown> = Record<string, unknown>

Constructors

  • Type Parameters

    • V = any
    • O extends Record<string, unknown> = Record<string, unknown>

    Parameters

    • options: CacheOptions = {}

    Returns default<V, O>

Methods

  • Parameters

    • key: string
    • valueToSet: V | (() => V)
    • OptionalpropsOrOptions: CacheMatchProps<O> | AddOptions<O>
    • OptionalonlyFull: boolean
    • OptionalexpirationTime: number

    Returns V

  • Caching only a non-empty value.

    Useful for get or create strategy

    Parameters

    • key: string
    • valueToSet: V | (() => V)
    • OptionalpropsOrOptions: CacheMatchProps<O> | AddOptions<O>
    • OptionalexpirationTime: number

    Returns V

    const cache = new Cache();
    const getItemFunc = () => fetch(url).then((data) => {
    return data.json(); // undefined
    });
    const item = await cache.addFull('foo', getItemFunc);
    if (!item) {
    await createItem(); // 'New item'
    }

    // somewhere else in the code
    const item = await cache.addFull('foo', getItemFunc).then((resp) => {
    console.log(resp); // 'New item'
    });
  • Parameters

    • item: CacheItem<any, any>

    Returns void

  • Parameters

    • key: string
    • Optionalprops: CacheMatchProps<O>

    Returns void

  • Parameters

    • key: string
    • Optionalprops: CacheMatchProps<O>

    Returns undefined | V

  • Parameters

    • Optionalkey: string
    • Optionalprops: CacheMatchProps<O>

    Returns V[]