expiring-types
v1.0.4
Published
Expiring map and expiring sets
Downloads
14
Maintainers
Readme
expiring-types
A lightweight TypeScript library providing data structures with automatic expiration functionality.
Installation
npm install expiring-typesFeatures
ExpiringMap- A Map-like data structure where entries expire after a specified timeExpiringSet- A Set-like data structure where elements expire after a specified time- Fully typed with TypeScript
- No dependencies
- Implements standard collection methods and iterators
Usage
ExpiringMap
import { ExpiringMap } from 'expiring-types';
// Create a map with entries that expire after 5000ms (5 seconds)
const cache = new ExpiringMap<string, any>(5000);
// Add items to the map
cache.set('user1', { name: 'John', age: 30 });
cache.set('user2', { name: 'Jane', age: 25 });
// Get an item
const user = cache.get('user1'); // Returns the user object
// Check if an item exists
if (cache.has('user1')) {
console.log('User exists in cache');
}
// After 5 seconds, the entries will be automatically removed
setTimeout(() => {
console.log(cache.has('user1')); // false
console.log(cache.size); // 0
}, 6000);
// Manually delete an entry
cache.delete('user1');
// Clear all entries
cache.clear();ExpiringSet
import { ExpiringSet } from 'expiring-types';
// Create a set with elements that expire after 10000ms (10 seconds)
const recentUsers = new ExpiringSet<string>(10000);
// Add items to the set
recentUsers.add('user123');
recentUsers.add('user456');
// Check if an element exists
if (recentUsers.has('user123')) {
console.log('User recently active');
}
// After 10 seconds, the elements will be automatically removed
setTimeout(() => {
console.log(recentUsers.has('user123')); // false
console.log(recentUsers.size); // 0
}, 11000);
// Iterate over the set
for (const user of recentUsers) {
console.log(user);
}
// Manually delete an element
recentUsers.delete('user123');
// Clear all elements
recentUsers.clear();API Reference
ExpiringMap<K, V>
A Map-like collection where each entry expires after a specified time.
Constructor
constructor(ttl: number)- Creates a new ExpiringMap with the specified time-to-live in milliseconds
Methods
set(key: K, value: V): this- Adds or updates an entryget(key: K): V | undefined- Retrieves an entry's valuehas(key: K): boolean- Checks if an entry existsdelete(key: K): boolean- Removes an entryclear(): void- Removes all entrieskeys(): IterableIterator<K>- Returns an iterator of all keysvalues(): IterableIterator<V>- Returns an iterator of all valuesentries(): IterableIterator<[K, V]>- Returns an iterator of all [key, value] pairs
Properties
size: number- The number of entries in the map
ExpiringSet
A Set-like collection where each element expires after a specified time.
Constructor
constructor(ttl: number)- Creates a new ExpiringSet with the specified time-to-live in milliseconds
Methods
add(value: T): this- Adds an elementhas(value: T): boolean- Checks if an element existsdelete(value: T): boolean- Removes an elementclear(): void- Removes all elementsvalues(): IterableIterator<T>- Returns an iterator of all valueskeys(): IterableIterator<T>- Returns an iterator of all valuesentries(): IterableIterator<[T, T]>- Returns an iterator of all [value, value] pairs
Properties
size: number- The number of elements in the set
License
MIT
