Class TtlCache<K, V>Experimental

Time-to-live cache.

UNSTABLE: New API, yet to be vetted.

Automatically removes entries after the configured amount of time elapses.

import { TtlCache } from "@std/cache/ttl-cache";
import { assertEquals } from "@std/assert/equals";
import { delay } from "@std/async/delay";

const cache = new TtlCache<string, number>(1000);

cache.set("a", 1);
assertEquals(cache.size, 1);
await delay(2000);
assertEquals(cache.size, 0);

Type Parameters

  • K

    The type of the cache keys.

  • V

    The type of the cache values.

Implements
Hierarchy
  • Map<K, V>
    • TtlCache

Constructors

  • Experimental

    Constructs a new instance.

    UNSTABLE: New API, yet to be vetted.

    Type Parameters

    • K
    • V

    Parameters

    • defaultTtl: number

      The default time-to-live in milliseconds. This value must be equal to or greater than 0. Its limit is determined by the current runtime's setTimeout implementation.

    Returns TtlCache<K, V>

Properties

[toStringTag]: string
size: number

the number of elements in the Map.

[species]: MapConstructor

Methods

  • Experimental

    Automatically clears all remaining timeouts once the cache goes out of scope if the cache is declared with using.

    UNSTABLE: New API, yet to be vetted.

    Returns void

    import { TtlCache } from "@std/cache/ttl-cache";
    import { assertEquals } from "@std/assert/equals";

    let c: TtlCache<string, number>;
    {
    using cache = new TtlCache<string, number>(1000);
    cache.set("a", 1);
    c = cache;
    }
    assertEquals(c.size, 0);
  • Experimental

    Returns an iterable of entries in the map.

    Returns MapIterator<[K, V]>

  • Experimental

    Clears the cache.

    UNSTABLE: New API, yet to be vetted.

    Returns void

    import { TtlCache } from "@std/cache";
    import { assertEquals } from "@std/assert/equals";

    const cache = new TtlCache<string, number>(1000);

    cache.set("a", 1);
    cache.set("b", 2);
    cache.clear();
    assertEquals(cache.size, 0);
  • Experimental

    Deletes the value associated with the given key.

    UNSTABLE: New API, yet to be vetted.

    Parameters

    • key: K

      The key to delete.

    Returns boolean

    true if the key was deleted, false otherwise.

    import { TtlCache } from "@std/cache";
    import { assertEquals } from "@std/assert/equals";

    const cache = new TtlCache<string, number>(1000);

    cache.set("a", 1);
    cache.delete("a");
    assertEquals(cache.has("a"), false);
  • Experimental

    Returns an iterable of key, value pairs for every entry in the map.

    Returns MapIterator<[K, V]>

  • Experimental

    Executes a provided function once per each key/value pair in the Map, in insertion order.

    Parameters

    • callbackfn: ((value: V, key: K, map: Map<K, V>) => void)
        • (value, key, map): void
        • Parameters

          • value: V
          • key: K
          • map: Map<K, V>

          Returns void

    • OptionalthisArg: any

    Returns void

  • Experimental

    Returns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.

    Parameters

    • key: K

    Returns undefined | V

    Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.

  • Experimental

    Parameters

    • key: K

    Returns boolean

    boolean indicating whether an element with the specified key exists or not.

  • Experimental

    Returns an iterable of keys in the map

    Returns MapIterator<K>

  • Experimental

    Set a value in the cache.

    UNSTABLE: New API, yet to be vetted.

    Parameters

    • key: K

      The cache key

    • value: V

      The value to set

    • ttl: number = ...

      A custom time-to-live. If supplied, overrides the cache's default TTL for this entry. This value must be equal to or greater than 0. Its limit is determined by the current runtime's setTimeout implementation.

    Returns this

    this for chaining.

    import { TtlCache } from "@std/cache/ttl-cache";
    import { assertEquals } from "@std/assert/equals";
    import { delay } from "@std/async/delay";

    const cache = new TtlCache<string, number>(100);

    cache.set("a", 1);
    assertEquals(cache.get("a"), 1);

    await delay(200);
    assertEquals(cache.get("a"), undefined);
  • Experimental

    Returns an iterable of values in the map

    Returns MapIterator<V>

  • Experimental

    Groups members of an iterable according to the return value of the passed callback.

    Type Parameters

    • K
    • T

    Parameters

    • items: Iterable<T, any, any>

      An iterable.

    • keySelector: ((item: T, index: number) => K)

      A callback which will be invoked for each item in items.

        • (item, index): K
        • Parameters

          • item: T
          • index: number

          Returns K

    Returns Map<K, T[]>