Class ExpiringMap<K, V>

A map that can have a capacity limit. The elapsed time for each get/set operation is maintained allowing for items to be automatically removed. has() does not affect the last access time.

By default, it uses the none eviction policy, meaning that when full an error will be thrown if attempting to add new keys.

Eviction policies: oldestGet removes the item that hasn't been accessed the longest, oldestSet removes the item that hasn't been updated the longest.

const map = new ExpiringMap();
map.set(`fruit`, `apple`);

// Remove all entries that were set more than 100ms ago
map.deleteWithElapsed(100, `set`);
// Remove all entries that were last accessed more than 100ms ago
map.deleteWithElapsed(100, `get`);
// Returns the elapsed time since `fruit` was last accessed
map.elapsedGet(`fruit`);
// Returns the elapsed time since `fruit` was last set
map.elapsedSet(`fruit`);

Last set/get time for a key can be manually reset using touch.

Events:

  • 'expired': when an item is automatically removed.
  • 'removed': when an item is manually or automatically removed.
  • 'newKey': when a new key is added
map.addEventListener(`expired`, evt => {
const { key, value } = evt;
});

The map can automatically remove items based on elapsed intervals.

Automatically delete items that haven't been accessed for one second

const map = new ExpiringMap({
autoDeleteElapsed: 1000,
autoDeletePolicy: `get`
});

Automatically delete the oldest item if we reach a capacity limit

const map = new ExpiringMap({
capacity: 5,
evictPolicy: `oldestSet`
});

Type Parameters

  • K

    Type of keys

  • V

    Type of values

Hierarchy (view full)

Constructors

Accessors

  • get isDisposed(): boolean
  • Returns boolean

  • get keyLength(): number
  • Returns the number of keys being stored.

    Returns number

Methods

  • Clears the contents of the map. Note: does not fire removed event

    Returns void

  • Deletes the value under key, if present.

    Returns true if something was removed.

    Parameters

    • key: K

    Returns boolean

  • Deletes all values where elapsed time has past for get/set or either.

    // Delete all keys (and associated values) not accessed for a minute
    em.deleteWithElapsed({mins:1}, `get`);
    // Delete things that were set 1s ago
    em.deleteWithElapsed(1000, `set`);

    Parameters

    • interval: Interval

      Interval

    • property: "get" | "set" | "either"

      Basis for deletion 'get','set' or 'either'

    Returns [k: K, v: V][]

    Items removed

  • Returns the elapsed time since key was accessed. Returns undefined if key does not exist

    Parameters

    • key: K

    Returns undefined | number

  • Returns the elapsed time since key was set. Returns undefined if key does not exist

    Parameters

    • key: K

    Returns undefined | number

  • Returns IterableIterator<[k: K, v: V]>

  • Gets an item from the map by key, returning undefined if not present

    Parameters

    • key: K

      Key

    Returns undefined | V

    Value, or undefined

  • Returns true if key is stored. Does not affect the key's last access time.

    Parameters

    • key: K

    Returns boolean

  • Returns IterableIterator<K>

  • Sets the key to be value.

    If the key already exists, it is updated.

    If the map is full, according to its capacity, another value is selected for removal.

    Parameters

    • key: K
    • value: V

    Returns void

  • Updates the lastSet/lastGet time for a value under k.

    Returns false if key was not found

    Parameters

    • key: K

    Returns boolean

  • Returns IterableIterator<V>