Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/jzszdznzzl/WABotJS/llms.txt

Use this file to discover all available pages before exploring further.

Utils.TTLCache<V> is a generic in-memory cache where every entry carries an expiry timestamp. An entry is considered expired once Date.now() surpasses insertTime + ttl. Expiry is not checked by get() — it returns the stored value regardless of whether the entry has expired. Expiry is enforced by has() (which returns false for expired entries) and by the bulk-read methods keys(), values(), and entries(), all of which filter out expired entries before returning results. A background setInterval cleaner also runs at the same interval as the TTL to sweep expired entries from the underlying Map, keeping memory usage bounded even when entries are never explicitly read.

Import & construction

import { Utils } from 'wabotjs';

const cache = new Utils.TTLCache<string>(60_000); // 60-second TTL

Constructor

new TTLCache<V>(ttl: number)
ttl
number
required
Time-to-live in milliseconds. Every entry inserted with set() expires after this many milliseconds. Must be a positive number — throws TypeError if the value is not a number or is less than 1.

Properties

size

get size(): number
Returns the number of non-expired entries currently in the cache. Expired entries that the background cleaner has not yet removed are excluded from this count.
const cache = new Utils.TTLCache<number>(5_000); // 5s TTL
cache.set('a', 1);
cache.set('b', 2);
console.log(cache.size); // 2

Methods

set(key, value)

set(key: string, value: V): this
Inserts or overwrites a key-value pair and records a fresh expiry timestamp (Date.now() + ttl). Also starts the background cleaner if it is not already running. Returns this for chaining.
key
string
required
Cache key. Must be a string — throws TypeError otherwise.
value
V
required
Value to store.
cache.set('session', 'abc123').set('token', 'xyz789');

get(key)

get(key: string): V | undefined
Returns the stored value for key regardless of expiry, or undefined if the key does not exist.
key
string
required
Cache key to look up.
get() returns a value even if the entry has expired. Use has() first when you need to confirm an entry is still live, or rely on entries() / keys() / values() which all filter expired entries automatically.
const value = cache.get('session'); // 'abc123' — returned regardless of expiry

del(key)

del(key: string): boolean
Immediately removes the entry with the given key from the cache. Returns true if the key existed (regardless of expiry), false if it was not found.
key
string
required
Cache key to delete.
cache.del('session'); // true

has(key)

has(key: string): boolean
Returns true only if the key exists and its expiry timestamp has not yet passed. Expired entries return false even if the cleaner has not yet removed them from the underlying Map.
console.log(cache.has('token')); // true (within TTL)

// After TTL elapses:
console.log(cache.has('token')); // false

clear()

clear(): void
Removes all entries from the cache and stops the background cleaner interval. The cleaner will restart automatically the next time set() is called.
cache.clear();
console.log(cache.size); // 0

keys()

keys(): string[]
Returns an array of keys for all non-expired entries. Expired entries are excluded even if the cleaner has not yet swept them.
console.log(cache.keys()); // ['token'] (if 'session' expired)

values()

values(): V[]
Returns an array of values for all non-expired entries, in the same order as keys().
console.log(cache.values()); // ['xyz789']

entries()

entries(): [string, V][]
Returns an array of [key, value] tuples for all non-expired entries.
console.log(cache.entries()); // [['token', 'xyz789']]

Background cleaner

The TTLCache starts a setInterval cleaner the first time set() is called. The interval period equals the TTL. On each tick, every entry whose expiry timestamp has passed is removed from the underlying Map. When the Map is fully empty, the cleaner stops itself automatically.
The cleaner interval is unref’d via interval.unref(). This means the interval will not keep the Node.js event loop alive on its own — your process can exit normally even if the cache still holds entries with a pending cleaner.
// The cleaner stops itself when the cache drains to zero entries.
// It restarts on the next call to set().
const shortCache = new Utils.TTLCache<string>(200); // 200ms TTL
shortCache.set('temp', 'value');

setTimeout(() => {
  // After 200ms the entry has expired; the cleaner will have removed it.
  console.log(shortCache.has('temp')); // false
}, 300);

Build docs developers (and LLMs) love