Class AntCache

Get stated

ES Module

import { AntCache } from 'ant-cache';

Common JS

const { AntCache } = require('ant-cache');

Usage

Use cache with TTL

// initialize using default config
const cache = new AntCache();

// initialize with config
const cache = new AntCache({
ttl: 120, // in seconds
checkPeriod: 10, // in seconds
maxKeys: 1000, // could hold maximum 1000 key-value pairs
})

Use as an enhanced JS Map (no TTL)

const cache = new AntCache({
checkPeriod: 0
});

Hierarchy

  • AntCache

Constructors

  • Parameters

    • Optional config: AntCacheConfig

      if no arg passed in, use defaultAntCacheConfig

      const DEFAULT_CHECK_PERIOD = 30;
      export const defaultAntCacheConfig: AntCacheConfig = {
      checkPeriod: DEFAULT_CHECK_PERIOD,
      ttl: 2 * DEFAULT_CHECK_PERIOD,
      maxKeys: 0,
      };

    Returns AntCache

Properties

_checkPeriodDisabled: boolean = false

true if config.checkPeriod = 0

_createdTsMap: Map<string, number>

Store the created timestamp of every key.

Must be synced with the main cache every time a new value is set.

The event emitter

_hits: number = 0

Cache hits

_misses: number = 0

Cache misses

_timerId: Timer

The interval timer

_ttlMap: Map<string, number>

Store the ttl of every key.

Must be synced with the main cache every time a new value is set.

close: (() => void) = ...

Type declaration

    • (): void
    • Alias for dispose

      Returns void

The config of the cache

Initialized once in the constructor, then read-only

mainCache: Map<string, AntCacheValue>

The main store that interacts with the outside world.

Methods to get the cache info, like size and keys, apply on this.

Methods

  • Delete a key from maps in sync.

    For internal use only.

    Parameters

    • key: string

    Returns void

  • Delete a single key

    Note: this method emits 'before-delete' hook, then deletes the key, then emits 'after-delete' hook.

    Parameters

    • key: string

    Returns void

  • Delete multiple keys

    Parameters

    • Rest ...args: (string | string[])[]

    Returns void

  • Parse the input JSON string and attempt to upsert into the current cache.

    JSON string should be the output of method serialize.

    Existing keys will be overwritten both value and TTL. Created timestamps will be reset.

    Use with caution as the bigger the string is, the longer it takes to parse.

    Leave logic handling long-running task up to you.

    Returns

    Parameters

    • json: string

    Returns void

  • Prepare to close the cache.

    Dispose the timer and the remove all event listeners.

    Returns void

  • Retrieve values of key groups

    Returns

    an object contains values of keys

    Parameters

    • Rest ...args: (string | string[])[]

    Returns Record<string, AntCacheValue>

  • This method is called every checkPeriod second interval to find and handle expired keys.

    Returns void

  • Check if the cache contains key

    Returns

    true if key in the cache, otherwise false

    Parameters

    • key: string

    Returns boolean

  • Adds a listener at the end of the listeners array for the specified event

    Parameters

    • eventName: AntCacheEvent

      an event name comply to AntCacheEvent

    • callback: ((...args: any[]) => void)

      a callback that will be call when the event emitted

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns void

  • Stringify the cache content and ttl.

    Use with caution as the bigger the cache is, the longer it takes to stringify.

    Leave the logic handling long-running task up to you.

    Returns

    JSON string to be consumed in deserialize

    Returns string

  • Insert or update a key.

    const cache = new AntCache({ ttl: 4, checkPeriod: 1 })

    // use default TTL
    cache.set('default ttl', 'live for 4 seconds');

    // use custom TTL
    cache.set('custom ttl', 'live for 10 seconds', 10);

    // live permanently
    cache.set('no ttl', 'this will live forever unless be deleted manually', 0);

    Parameters

    • key: string

      should be string

    • val: AntCacheValue
    • ttl: number = ...

      in seconds, only take effect when inserting a new key. If ttl = 0, there is no TTL at all

    Returns void

Generated using TypeDoc