/**
* Simple in-memory cache utility
* Stores data with TTL (time to live)
*/
interface CacheEntry {
data: unknown;
expiresAt: number;
}
const cache = new Map<string, CacheEntry>();
/**
* Get value from cache
* @param key - Cache key
* @returns Cached value or null if not found or expired
*/
export const getCache = <T>(key: string): T | null => {
const entry = cache.get(key);
if (!entry) {
return null;
}
// Check if expired
if (Date.now() > entry.expiresAt) {
cache.delete(key);
return null;
}
return entry.data as T;
};
/**
* Set value in cache
* @param key - Cache key
* @param value - Value to cache
* @param ttlMs - Time to live in milliseconds
*/
export const setCache = <T>(key: string, value: T, ttlMs: number): void => {
cache.set(key, {
data: value,
expiresAt: Date.now() + ttlMs,
});
};
/**
* Delete value from cache
* @param key - Cache key
*/
export const deleteCache = (key: string): void => {
cache.delete(key);
};
/**
* Clear all cache
*/
export const clearCache = (): void => {
cache.clear();
};