InterfaceIAsyncCache<Payload, Key>

Asynchronous cache interface

v0.0.1

function foo(cache: IAsyncCache<string>) {
const value = await cache.get('key');
await cache.set('key', 'value');
await cache.has('key'); // true
await cache.delete('key');
await cache.clear();
await cache.size(); // 0
}
interface IAsyncCache<Payload, Key> {
    clear(): void | Promise<void>;
    delete(key: Key): boolean | Promise<boolean>;
    entries(): IterableIterator<[Key, Payload], any, any> | AsyncIterableIterator<[Key, Payload], any, any>;
    expires(key: Key): undefined | Date | Promise<undefined | Date>;
    get(key: Key): undefined | Payload | Promise<undefined | Payload>;
    has(key: Key): boolean | Promise<boolean>;
    keys(): IterableIterator<Key, any, any> | AsyncIterableIterator<Key, any, any>;
    set(key: Key, value: Payload, expires?: Date): void | Promise<void>;
    size(): number | Promise<number>;
    values(): IterableIterator<Payload, any, any> | AsyncIterableIterator<Payload, any, any>;
}

Type Parameters

  • Payload

    The type of the cached value

  • Key = string

    Optional type of the cache key (default is string)

Methods

  • Clear all cached values

    Returns void | Promise<void>

  • Deletes a value from the cache

    Parameters

    • key: Key

      The key to delete the value for

    Returns boolean | Promise<boolean>

    Promise of true if the value was deleted, false otherwise

  • Returns an async iterator of key and value pairs for every entry in the cache.

    Returns IterableIterator<[Key, Payload], any, any> | AsyncIterableIterator<[Key, Payload], any, any>

    for await (const [key, value] of cache.entries()) {
    console.log(key, value);
    }
  • Get key expiration Date object or undefined if not found in cache

    Parameters

    • key: Key

      The key to get the expiration for

    Returns undefined | Date | Promise<undefined | Date>

    Promise of Date object or undefined if not found in cache

  • Gets a value from the cache

    Parameters

    • key: Key

      The key to get the value for

    Returns undefined | Payload | Promise<undefined | Payload>

    Promise of the cached value or undefined if not found

  • Checks if a key exists in the cache

    Parameters

    • key: Key

      The key to check for

    Returns boolean | Promise<boolean>

    Promise of true if the key exists in the cache, false otherwise

  • Async iterator for cache keys

    Returns IterableIterator<Key, any, any> | AsyncIterableIterator<Key, any, any>

    for await (const key of cache.keys()) {
    console.log(key);
    }
  • Sets a value in the cache with an optional expiration date

    Parameters

    • key: Key

      The key to set the value for

    • value: Payload

      The value to set in the cache

    • Optionalexpires: Date

      The optional expiration date for the cache entry

    Returns void | Promise<void>

    Promise of void

  • Gets the number of items in the cache

    Returns number | Promise<number>

    Promise of the number of items in the cache

  • Async iterator for cache values

    Returns IterableIterator<Payload, any, any> | AsyncIterableIterator<Payload, any, any>

    for await (const value of cache.values()) {
    console.log(value);
    }