File size: 11,314 Bytes
8de1fe2
 
 
 
 
 
 
 
 
 
 
4a2d311
8de1fe2
 
4a2d311
8de1fe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af171a8
 
 
 
8de1fe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af171a8
8de1fe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
787d56f
 
 
 
43582fd
507f1c1
43582fd
 
507f1c1
 
43582fd
507f1c1
8de1fe2
 
 
 
 
 
 
 
507f1c1
 
 
 
8de1fe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// ============================================
// 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);
}