pixelpinch-proxy / src /cache.js
Pixel Pinch Extensions
fix: bug fixes across compress, cache, and server
4a2d311
Raw History Blame Contribute Delete
11.3 kB
// ============================================
// CACHE MANAGER CONFIGURATION
// ============================================
import CONFIG from './config.js';
import { cacheLogger } from './logger.js';
import { createCache } from 'cache-manager';
import { isBunRuntime, fastHash } from './utils/bun-utils.js';
// Cache manager configuration for cache-manager 7
const cacheConfig = {
ttl: CONFIG.CACHE_TTL, // Already in seconds (180 = 3 minutes)
max: 100, // Reduced from 1000 to prevent memory issues
updateAgeOnGet: true, // Refresh TTL when accessed
checkPeriod: CONFIG.CACHE_TTL, // Period to check for expired items (seconds)
};
// Cache instance (will be initialized asynchronously)
let cache = null;
// Initialize cache function
async function initializeCache() {
try {
cache = await createCache(cacheConfig);
// Don't reset cache during initialization - it's already empty
cacheLogger.debug('Cache manager initialized', { config: cacheConfig });
return true;
} catch (err) {
cacheLogger.error('Cache initialization failed', { error: err.message });
return false;
}
}
// Cache statistics
const cacheStats = {
hits: 0,
misses: 0,
sets: 0,
deletes: 0,
errors: 0
};
// Enhanced cache wrapper with logging and statistics
export const cacheWrapper = {
// Get item from cache
async get(key) {
if (!cache) {
cacheLogger.warn('Cache not initialized, skipping get operation');
return undefined;
}
try {
const result = await cache.get(key);
if (result !== undefined) {
cacheStats.hits++;
cacheLogger.debug('Cache hit', { key });
return result;
}
cacheStats.misses++;
cacheLogger.debug('Cache miss', { key });
return undefined;
} catch (err) {
cacheStats.errors++;
cacheLogger.error('Cache get error', { key, error: err.message });
return undefined;
}
},
// Set item in cache
async set(key, value, ttlSeconds = CONFIG.CACHE_TTL) {
if (!cache) {
cacheLogger.warn('Cache not initialized, skipping set operation');
return false;
}
try {
await cache.set(key, value, { ttl: ttlSeconds });
cacheStats.sets++;
cacheLogger.debug('Cache set', { key, ttl: ttlSeconds });
return true;
} catch (err) {
cacheStats.errors++;
cacheLogger.error('Cache set error', { key, error: err.message });
return false;
}
},
// Delete item from cache
async del(key) {
if (!cache) {
cacheLogger.warn('Cache not initialized, skipping delete operation');
return false;
}
try {
await cache.del(key);
cacheStats.deletes++;
cacheLogger.debug('Cache delete', { key });
return true;
} catch (err) {
cacheStats.errors++;
cacheLogger.error('Cache delete error', { key, error: err.message });
return false;
}
},
// Clear all cache
async reset() {
if (!cache) {
cacheLogger.warn('Cache not initialized, skipping reset operation');
return false;
}
try {
// Check if reset method exists (cache-manager v7+ might not have it)
if (typeof cache.reset === 'function') {
await cache.reset();
} else if (typeof cache.clear === 'function') {
await cache.clear();
} else {
// Alternative: recreate cache instance
cacheLogger.warn('Cache reset method not available, recreating cache');
try {
cache = await createCache(cacheConfig);
} catch (recreateError) {
cacheLogger.error('Failed to recreate cache', { error: recreateError.message });
return false;
}
}
// Trigger garbage collection after cache reset (Bun optimization)
if (isBunRuntime() && typeof Bun.gc === 'function') {
Bun.gc(false);
cacheLogger.debug('Triggered GC after cache reset');
}
cacheLogger.debug('Cache reset');
return true;
} catch (err) {
cacheStats.errors++;
cacheLogger.error('Cache reset error', { error: err.message });
return false;
}
},
// Check if key exists
async has(key) {
if (!cache) {
cacheLogger.warn('Cache not initialized, skipping has operation');
return false;
}
try {
const result = await cache.get(key);
return result !== undefined;
} catch (err) {
cacheStats.errors++;
cacheLogger.error('Cache has error', { key, error: err.message });
return false;
}
},
// Get cache statistics
getStats() {
const totalRequests = cacheStats.hits + cacheStats.misses;
const baseStats = {
...cacheStats,
size: cache?.store?.size || cache?.keys?.length || 0,
hitRate: totalRequests > 0
? (cacheStats.hits / totalRequests * 100).toFixed(2) + '%'
: '0%'
};
// Add Bun-specific memory stats
if (isBunRuntime()) {
const mem = process.memoryUsage();
baseStats.memoryUsage = Math.round(mem.heapUsed / 1024 / 1024) + 'MB';
baseStats.runtime = 'Bun';
}
return baseStats;
},
// Normalize Accept header to extract format support flags
normalizeAcceptHeader(acceptHeader) {
if (!acceptHeader || typeof acceptHeader !== 'string' || acceptHeader.trim() === '') {
return 'empty'; // Mobile apps with no Accept header
}
const normalized = acceptHeader.toLowerCase();
const supportsAvif = normalized.includes('image/avif');
const supportsWebp = normalized.includes('image/webp');
// Create a consistent normalized representation
const parts = [];
if (supportsAvif) parts.push('avif');
if (supportsWebp) parts.push('webp');
// If neither AVIF nor WebP, mark as basic (JPEG fallback)
return parts.length > 0 ? parts.join(',') : 'basic';
},
// Generate cache key
generateKey(url, query, acceptHeader = '') {
const { w, h, q, format, avif, webp, jpeg, preset } = query || {};
const normalizedAccept = this.normalizeAcceptHeader(acceptHeader);
// Build a deterministic string representation of the key
// Order matters for consistency, so we build it explicitly
const keyParts = [];
if (url) keyParts.push(`url:${url}`);
if (w) keyParts.push(`w:${w}`);
if (h) keyParts.push(`h:${h}`);
if (q) keyParts.push(`q:${q}`);
if (format) keyParts.push(`format:${format}`);
if (avif) keyParts.push('avif:1');
if (webp) keyParts.push('webp:1');
if (jpeg) keyParts.push('jpeg:1');
if (preset) keyParts.push(`preset:${preset}`);
// Include normalized Accept header to ensure format-specific caching
keyParts.push(`accept:${normalizedAccept}`);
const keyString = keyParts.join('|');
// Use fast hash function for efficient cache keys
// This produces shorter keys and is faster than JSON.stringify
return fastHash(keyString);
},
// Check if response should be cached
shouldCache(result) {
// Don't cache if no compression achieved
if (!result || !result.originalSize || !result.size) {
return false;
}
const savings = result.originalSize - result.size;
const savingsPercent = (savings / result.originalSize) * 100;
// Only cache if we achieved meaningful compression
return savingsPercent >= CONFIG.MIN_SAVINGS_PERCENT;
},
// Initialize cache (call this during app startup)
async init() {
return await initializeCache();
}
};
// Export initialization function
export { initializeCache };
// Initialize cache on module load
initializeCache().catch(err => {
console.error('Failed to initialize cache:', err);
// Set cache to null to prevent further operations
cache = null;
});
// Periodic cache cleanup and stats logging with Bun optimization
let statsTimer = null;
let cacheClearTimer = null;
// Hourly cache clearing disabled
const setupHourlyCacheClear = () => {
// Cache clearing disabled - no automatic hourly clearance
cacheLogger.info('Hourly cache clearing is disabled');
};
// Hourly cache clearing initialization disabled
export default cache;
// Cleanup function to prevent memory leaks
export function cleanupCacheTimers() {
if (statsTimer) {
clearInterval(statsTimer);
statsTimer = null;
}
if (cacheClearTimer) {
clearInterval(cacheClearTimer);
cacheClearTimer = null;
}
}
if (isBunRuntime()) {
// Bun automatically optimizes standard setInterval
statsTimer = setInterval(async () => {
try {
const stats = cacheWrapper.getStats();
cacheLogger.debug('Cache statistics', stats);
// Memory-based auto cleanup (Bun optimization) - only check every 5 minutes
if (Math.random() < 0.2) { // 20% chance to reduce frequency
const mem = process.memoryUsage();
const memoryPressure = mem.heapUsed / mem.heapTotal;
if (memoryPressure > 0.8) {
cacheLogger.warn('High memory pressure detected', {
heapUsed: Math.round(mem.heapUsed / 1024 / 1024) + 'MB',
heapTotal: Math.round(mem.heapTotal / 1024 / 1024) + 'MB',
pressure: (memoryPressure * 100).toFixed(1) + '%'
});
await cacheWrapper.reset();
if (typeof Bun.gc === 'function') {
Bun.gc(true); // Aggressive GC
cacheLogger.debug('Triggered aggressive GC due to memory pressure');
}
}
}
} catch (error) {
cacheLogger.error('Cache stats timer error', { error: error.message });
}
}, 60000); // Log every minute
} else {
// Fallback for Node.js
statsTimer = setInterval(() => {
try {
const stats = cacheWrapper.getStats();
cacheLogger.debug('Cache statistics', stats);
} catch (error) {
cacheLogger.error('Cache stats timer error', { error: error.message });
}
}, 60000);
}