Class default<V, O>

Example

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

Example

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>

Hierarchy

  • default

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)
    • Optional props: CacheMatchProps<O>
    • Optional onlyFull: boolean

    Returns V

  • Caching only a non-empty value.

    Useful for get or create strategy

    Parameters

    • key: string
    • valueToSet: V | (() => V)
    • Optional props: CacheMatchProps<O>

    Returns V

    Example

    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

    • key: string
    • Optional props: CacheMatchProps<O>

    Returns undefined | V

Generated using TypeDoc