@oxyhq/services
Version:
Reusable OxyHQ module to handle authentication, user management, karma system, device-based session management and more 🚀
128 lines • 3.25 kB
TypeScript
/**
* Centralized cache utility with TTL support
*
* This is a production-ready cache implementation used across the codebase
* for consistent caching behavior and performance optimization.
*/
/**
* Cache statistics
*/
export interface CacheStats {
size: number;
hits: number;
misses: number;
hitRate: number;
}
/**
* TTL-based cache implementation
*
* Features:
* - Automatic expiration based on TTL
* - Manual cleanup of expired entries
* - Statistics tracking (hits, misses, hit rate)
* - Type-safe generic interface
*
* @example
* ```typescript
* const cache = new TTLCache<string>(5 * 60 * 1000); // 5 minutes
*
* // Set with default TTL
* cache.set('key', 'value');
*
* // Set with custom TTL
* cache.set('key', 'value', 10 * 60 * 1000); // 10 minutes
*
* // Get value
* const value = cache.get('key');
*
* // Get statistics
* const stats = cache.getStats();
* ```
*/
export declare class TTLCache<T> {
private cache;
private defaultTTL;
private hits;
private misses;
/**
* Create a new TTL cache
* @param defaultTTL Default TTL in milliseconds (default: 5 minutes)
*/
constructor(defaultTTL?: number);
/**
* Get a value from cache
* @param key Cache key
* @returns Cached value or null if not found or expired
*/
get(key: string): T | null;
/**
* Set a value in cache
* @param key Cache key
* @param data Data to cache
* @param ttl Optional TTL override (uses default if not provided)
*/
set(key: string, data: T, ttl?: number): void;
/**
* Delete a specific cache entry
* @param key Cache key
* @returns true if entry was deleted, false if not found
*/
delete(key: string): boolean;
/**
* Clear all cache entries
*/
clear(): void;
/**
* Check if a key exists and is not expired
* @param key Cache key
* @returns true if key exists and is valid
*/
has(key: string): boolean;
/**
* Get all valid cache keys
* @returns Array of valid cache keys
*/
keys(): string[];
/**
* Clean up expired entries
* @returns Number of entries removed
*/
cleanup(): number;
/**
* Get cache size (number of entries)
*/
size(): number;
/**
* Get cache statistics
*/
getStats(): CacheStats;
/**
* Reset statistics (keeps cache entries)
*/
resetStats(): void;
}
/**
* Create a TTL cache instance (convenience function)
*
* @example
* ```typescript
* const cache = createCache<string>(5 * 60 * 1000);
* ```
*/
export declare function createCache<T>(ttl?: number): TTLCache<T>;
/**
* Register a cache for automatic cleanup
* @param cache Cache instance to register
*/
export declare function registerCacheForCleanup(cache: TTLCache<any>): void;
/**
* Unregister a cache from automatic cleanup
* @param cache Cache instance to unregister
*/
export declare function unregisterCacheFromCleanup(cache: TTLCache<any>): void;
/**
* Stop all cleanup intervals (useful for testing)
* This will clear the interval and unregister all caches
*/
export declare function stopAllCleanupIntervals(): void;
//# sourceMappingURL=cache.d.ts.map