Skip to content

LimitedCollection

Represents a collection that holds at most `limit` entries, evicting the oldest ones first, and whose entries can expire after `expire` milliseconds.

Signature

export declare class LimitedCollection<K, V>
JSDoc
@templateK The type of the keys in the collection.@templateV The type of the values in the collection.

Properties

private readonly data;
closerGetter
get closer(): {    value: V;    expire: number;    expireOn: number;} | undefined;

Returns the element in the limited collection that is closest to expiration.

Returns

The element that is closest to expiration, or `undefined` if the collection is empty.

Examples
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one', 1000);
collection.set(2, 'two', 2000);
collection.set(3, 'three', 500);
const closestElement = collection.closer;
console.log(closestElement); // Output: { value: 'three', expire: 500, expireOn: [current timestamp + 500] }
sizeGetter
get size(): number;

Returns the number of elements in the limited collection.

Returns

The number of elements in the collection.

Examples
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one');
collection.set(2, 'two');
console.log(collection.size); // Output: 2

Methods

set(key: K, value: V, customExpire?: number): boolean;

Adds an element to the limited collection.

Parameters
key
The key of the element.
value
The value of the element.
customExpire
The custom expiration time for the element.
Returns

Whether the configured limit accepted the entry. Reentrant mutations from `onDelete` do not change this result.

Examples
const collection = new LimitedCollection<number, string>({ limit: 3 });
collection.set(1, 'one');
collection.set(2, 'two');
collection.set(3, 'three');
console.log(collection.size); // Output: 3
collection.set(4, 'four');
console.log(collection.size); // Output: 3
console.log(collection.get(1)); // Output: undefined
raw(key: K): {    value: V;    expire: number;    expireOn: number;} | undefined;

Returns the raw data of an element in the limited collection.

Parameters
key
The key of the element.
Returns

The raw data of the element, or `undefined` if the element does not exist.

Examples
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one');
const rawData = collection.raw(1);
console.log(rawData); // Output: { value: 'one', expire: -1, expireOn: -1 }
get(key: K): V | undefined;

Returns the value of an element in the limited collection.

Parameters
key
The key of the element.
Returns

The value of the element, or `undefined` if the element does not exist.

Examples
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one');
const value = collection.get(1);
console.log(value); // Output: 'one'
has(key: K): boolean;

Checks if an element exists in the limited collection.

Parameters
key
The key of the element.
Returns

`true` if the element exists, `false` otherwise.

Examples
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one');
console.log(collection.has(1)); // Output: true
console.log(collection.has(2)); // Output: false
delete(key: K): boolean;

Removes an element from the limited collection.

Parameters
key
The key of the element to remove.
Returns

`true` if the element was removed, `false` otherwise.

Examples
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one');
console.log(collection.delete(1)); // Output: true
console.log(collection.delete(2)); // Output: false
keys(): MapIterator<K>;
values(): IterableIterator<V>;
entries(): IterableIterator<[    K,    V]>;