Class TtlCache<K, V>Experimental
Type Parameters
Implements
Constructors
constructor
- new
Ttl <K, V>(defaultTtl): TtlCache<K, V>Cache ExperimentalConstructs a new instance.
UNSTABLE: New API, yet to be vetted.
Type Parameters
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>
- defaultTtl: number
Properties
Readonly Experimental[toStringTag]
Readonly Experimentalsize
Static Readonly Experimental[species]
Methods
[dispose]
[iterator]
clear
delete
- delete(key): boolean
ExperimentalDeletes the value associated with the given key.
UNSTABLE: New API, yet to be vetted.
Parameters
- key: K
The key to delete.
Returns boolean
trueif the key was deleted,falseotherwise.- key: K
entries
forEach
get
- get(key): undefined | V
ExperimentalReturns 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.
has
- has(key): boolean
ExperimentalParameters
- key: K
Returns boolean
boolean indicating whether an element with the specified key exists or not.
keys
set
- set(key, value, ttl?): this
ExperimentalSet a value in the cache.
UNSTABLE: New API, yet to be vetted.
Parameters
Returns this
thisfor chaining.Example: Usage
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);
Time-to-live cache.
UNSTABLE: New API, yet to be vetted.
Automatically removes entries after the configured amount of time elapses.
Example: Usage