InterfaceICache<Payload, Key>

Synchronous cache interface

v0.0.1

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

  • Deletes a value from the cache

    Parameters

    • key: Key

      The key to delete the value for

    Returns boolean

    True if the value was deleted, false otherwise

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

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

    for (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

    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

    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

    True if the key exists in the cache, false otherwise

  • Iterator for cache keys

    Returns IterableIterator<Key, any, any>

    for (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

  • Gets the number of items in the cache

    Returns number

    The number of items in the cache

  • Iterator for cache values

    Returns IterableIterator<Payload, any, any>

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