@catbee/utils
Version:
A modular, production-grade utility toolkit for Node.js and TypeScript, designed for robust, scalable applications (including Express-based services). All utilities are tree-shakable and can be imported independently.
270 lines • 7.96 kB
JavaScript
import { Config } from "../config";
/**
* An in-memory cache with time-to-live (TTL) support for each entry.
*
* @typeParam K - Type of the key.
* @typeParam V - Type of the value.
*
* @example
* const cache = new TTLCache<string, number>({ ttlMs: 1000 });
* cache.set("x", 123);
* const value = cache.get("x"); // 123
*/
export class TTLCache {
/**
* @param options - Configuration options for the cache
*/
constructor(options = {}) {
var _a;
this.cache = new Map();
this.ttlMs = (_a = options.ttlMs) !== null && _a !== void 0 ? _a : Config.Cache.defaultTtl;
this.maxSize = options.maxSize;
// Setup auto-cleanup if enabled
const autoCleanupMs = options.autoCleanupMs;
if (autoCleanupMs && autoCleanupMs > 0) {
this.cleanupInterval = setInterval(() => {
this.cleanup();
}, autoCleanupMs);
}
}
/**
* Sets a key-value pair in the cache with the default TTL.
*
* @param key - The key to set.
* @param value - The value to associate with the key.
*/
set(key, value) {
this.setWithTTL(key, value, this.ttlMs);
}
/**
* Sets a key-value pair in the cache with a custom TTL.
*
* @param key - The key to set.
* @param value - The value to associate with the key.
* @param ttlMs - Time-to-live in milliseconds.
*/
setWithTTL(key, value, ttlMs) {
const now = Date.now();
const expiresAt = now + ttlMs;
this.cache.set(key, { value, expiresAt, lastAccessed: now });
// Enforce max size limit if configured
if (this.maxSize && this.cache.size > this.maxSize) {
this.evictLRU();
}
}
/**
* Retrieves the value for a given key if it hasn't expired.
*
* @param key - The key to retrieve.
* @returns The cached value, or undefined if not found or expired.
*/
get(key) {
const entry = this.cache.get(key);
if (!entry)
return undefined;
const now = Date.now();
if (now > entry.expiresAt) {
this.cache.delete(key);
return undefined;
}
// Update last access time for LRU tracking
entry.lastAccessed = now;
return entry.value;
}
/**
* Retrieves or computes a value if it's not in the cache or has expired.
*
* @param key - The key to retrieve
* @param producer - Function to generate the value if not cached
* @param ttlMs - Optional custom TTL for the computed value
* @returns The cached or computed value
*/
async getOrCompute(key, producer, ttlMs) {
const value = this.get(key);
if (value !== undefined)
return value;
const newValue = await producer();
this.setWithTTL(key, newValue, ttlMs !== null && ttlMs !== void 0 ? ttlMs : this.ttlMs);
return newValue;
}
/**
* Checks if the key exists and hasn't expired.
*
* @param key - The key to check.
* @returns `true` if key exists and is valid, otherwise `false`.
*/
has(key) {
return this.get(key) !== undefined;
}
/**
* Deletes a key from the cache.
*
* @param key - The key to delete.
* @returns `true` if the key existed and was removed, `false` otherwise.
*/
delete(key) {
return this.cache.delete(key);
}
/**
* Clears all entries from the cache.
*/
clear() {
this.cache.clear();
}
/**
* Returns the number of entries currently in the cache (includes expired entries until next access/cleanup).
*
* @returns Number of items in the cache (may include expired keys).
*/
size() {
return this.cache.size;
}
/**
* Set multiple key-value pairs at once with the default TTL.
*
* @param entries - Array of [key, value] tuples to set
*/
setMany(entries) {
for (const [key, value] of entries) {
this.set(key, value);
}
}
/**
* Get multiple values at once.
*
* @param keys - Array of keys to retrieve
* @returns Array of values (undefined for keys that don't exist or expired)
*/
getMany(keys) {
return keys.map((key) => this.get(key));
}
/**
* Removes all expired entries from the cache.
*
* @returns Number of entries removed.
*/
cleanup() {
let removed = 0;
const now = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (now > entry.expiresAt) {
this.cache.delete(key);
removed++;
}
}
return removed;
}
/**
* Returns an iterator of all current valid [key, value] pairs.
*
* @returns IterableIterator<[K, V]>
*/
*entries() {
for (const [key, entry] of this.cache.entries()) {
if (Date.now() <= entry.expiresAt) {
yield [key, entry.value];
}
}
}
/**
* Returns an iterator of all current valid keys.
*
* @returns IterableIterator<K>
*/
*keys() {
for (const [key, entry] of this.cache.entries()) {
if (Date.now() <= entry.expiresAt) {
yield key;
}
}
}
/**
* Returns an iterator of all current valid values.
*
* @returns IterableIterator<V>
*/
*values() {
for (const entry of this.cache.values()) {
if (Date.now() <= entry.expiresAt) {
yield entry.value;
}
}
}
/**
* Extends the expiration of a key by the specified time or default TTL.
*
* @param key - The key to refresh
* @param ttlMs - Optional new TTL in milliseconds (uses default if not specified)
* @returns true if the key was found and refreshed, false otherwise
*/
refresh(key, ttlMs) {
const entry = this.cache.get(key);
if (!entry)
return false;
const now = Date.now();
if (now > entry.expiresAt) {
this.cache.delete(key);
return false;
}
entry.expiresAt = now + (ttlMs !== null && ttlMs !== void 0 ? ttlMs : this.ttlMs);
entry.lastAccessed = now;
return true;
}
/**
* Returns a snapshot of cache stats.
*
* @returns Object containing cache statistics
*/
stats() {
const now = Date.now();
let expired = 0;
let valid = 0;
for (const entry of this.cache.values()) {
if (now > entry.expiresAt) {
expired++;
}
else {
valid++;
}
}
return {
size: this.cache.size,
validEntries: valid,
expiredEntries: expired,
maxSize: this.maxSize,
};
}
/**
* Stop the auto-cleanup interval if it's running.
*/
destroy() {
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
this.cleanupInterval = undefined;
}
}
/**
* Evict the least recently used entry from the cache.
* @private
*/
evictLRU() {
let oldestKey;
let oldestAccess = Infinity;
for (const [key, entry] of this.cache.entries()) {
// Skip entries that are already expired
if (Date.now() > entry.expiresAt) {
this.cache.delete(key);
return;
}
// Find the oldest accessed entry
if (entry.lastAccessed && entry.lastAccessed < oldestAccess) {
oldestAccess = entry.lastAccessed;
oldestKey = key;
}
}
if (oldestKey) {
this.cache.delete(oldestKey);
}
}
}
//# sourceMappingURL=cache.utils.js.map