UNPKG

@j03fr0st/pubg-ts

Version:

A comprehensive TypeScript wrapper for the PUBG API

132 lines 4.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createCacheKey = exports.globalCache = exports.MemoryCache = void 0; const logger_1 = require("./logger"); class MemoryCache { constructor(options = {}) { this.cache = new Map(); this.hits = 0; this.misses = 0; this.defaultTtl = options.ttl ?? 5 * 60 * 1000; // 5 minutes default this.maxSize = options.maxSize ?? 1000; } set(key, data, ttl) { const expiration = ttl ?? this.defaultTtl; // Remove oldest entries if cache is full if (this.cache.size >= this.maxSize) { this.cleanup(); // If still full, remove oldest entry if (this.cache.size >= this.maxSize) { const oldestKey = this.cache.keys().next().value; if (oldestKey) { this.cache.delete(oldestKey); logger_1.logger.cache(`Evicted oldest entry: ${oldestKey}`); } } } const entry = { data, timestamp: Date.now(), ttl: expiration, }; this.cache.set(key, entry); logger_1.logger.cache(`Set cache entry: ${key} (TTL: ${expiration}ms)`); } get(key) { const entry = this.cache.get(key); if (!entry) { logger_1.logger.cache(`Cache miss: ${key}`); this.misses++; return undefined; } const now = Date.now(); const isExpired = now - entry.timestamp > entry.ttl; if (isExpired) { this.cache.delete(key); logger_1.logger.cache(`Cache expired: ${key}`); this.misses++; return undefined; } logger_1.logger.cache(`Cache hit: ${key}`); this.hits++; return entry.data; } has(key) { const entry = this.cache.get(key); if (!entry) { return false; } const now = Date.now(); const isExpired = now - entry.timestamp > entry.ttl; if (isExpired) { this.cache.delete(key); return false; } return true; } delete(key) { const deleted = this.cache.delete(key); if (deleted) { logger_1.logger.cache(`Cache entry deleted: ${key}`); } return deleted; } clear() { const size = this.cache.size; this.cache.clear(); logger_1.logger.cache(`Cache cleared: ${size} entries removed`); } cleanup() { const now = Date.now(); let removedCount = 0; for (const [key, entry] of this.cache.entries()) { const isExpired = now - entry.timestamp > entry.ttl; if (isExpired) { this.cache.delete(key); removedCount++; } } if (removedCount > 0) { logger_1.logger.cache(`Cache cleanup: ${removedCount} expired entries removed`); } } getStats() { const total = this.hits + this.misses; return { size: this.cache.size, maxSize: this.maxSize, defaultTtl: this.defaultTtl, hits: this.hits, misses: this.misses, hitRate: total > 0 ? this.hits / total : 0, }; } async warm(warmingEntries, ttl) { logger_1.logger.cache(`Warming cache with ${warmingEntries.length} entries...`); const promises = warmingEntries.map(async ({ key, value }) => { if (!this.has(key)) { try { const data = await value(); this.set(key, data, ttl); } catch (error) { logger_1.logger.error(`Failed to warm cache for key "${key}":`, error); } } }); await Promise.all(promises); logger_1.logger.cache('Cache warming complete.'); } } exports.MemoryCache = MemoryCache; // Global cache instance exports.globalCache = new MemoryCache({ ttl: 5 * 60 * 1000, // 5 minutes maxSize: 1000, }); // Cache key generators const createCacheKey = (prefix, ...parts) => { return `${prefix}:${parts.join(':')}`; }; exports.createCacheKey = createCacheKey; //# sourceMappingURL=cache.js.map