UNPKG

fortify2-js

Version:

MOST POWERFUL JavaScript Security Library! Military-grade cryptography + 19 enhanced object methods + quantum-resistant algorithms + perfect TypeScript support. More powerful than Lodash with built-in security.

246 lines (242 loc) 8.36 kB
'use strict'; var CacheFactory = require('../../../cache/CacheFactory.js'); var Logger = require('../../utils/Logger.js'); /** * CacheManager - Handles all cache-related operations for FastApi.ts * Manages cache creation, configuration, warming, and key generation */ class CacheManager { constructor(options) { this.options = options; this.cache = this.createCache(); } /** * Get the cache instance */ getCache() { return this.cache; } /** * Create cache instance with optimal configuration */ createCache() { const cacheStrategy = this.detectCacheStrategy(); const cacheConfig = { type: cacheStrategy, memory: this.options.cache?.memory, redis: this.options.cache?.redis, performance: { batchSize: this.options.performance?.batchSize, asyncWrite: this.options.performance?.asyncWrite, prefetchEnabled: this.options.performance?.prefetch, connectionPooling: this.options.performance?.connectionPooling, }, security: { encryption: true, // Default security settings accessMonitoring: true, sanitization: true, auditLogging: false, }, monitoring: { enabled: true, detailed: false, alertThresholds: undefined, }, }; return CacheFactory.createOptimalCache(cacheConfig); } /** * Auto-detect optimal cache strategy */ detectCacheStrategy() { if (this.options.cache?.strategy && this.options.cache.strategy !== "auto") { return this.options.cache.strategy; } const hasRedis = this.options.cache?.redis?.host || process.env.REDIS_URL || process.env.REDIS_HOST; const memoryLimit = this.options.cache?.memory?.maxSize || 100; const isMemoryConstrained = memoryLimit < 50; if (hasRedis && !isMemoryConstrained) { return "hybrid"; } else if (hasRedis) { return "redis"; } else { return "memory"; } } /** * Initialize cache connection */ async initializeCache() { try { await this.cache.connect(); } catch (error) { Logger.logger.error("cache", "Cache initialization failed:", error); throw error; } } /** * ULTRA-FAST CACHE WARMING - Pre-populate cache with instant responses */ async warmUpUltraFastCache() { try { // Get system info from configuration for library-agnostic responses const systemInfo = { serviceName: this.options.server?.serviceName || "FastApi.ts Service", version: this.options.server?.version || "1.0.0", environment: this.options.env || process.env.NODE_ENV || "development", }; // Pre-populate ultra-fast responses with configurable data const ultraFastResponses = [ { key: "ultra:GET:/health", value: { status: "ok", timestamp: Date.now(), service: systemInfo.serviceName, version: systemInfo.version, environment: systemInfo.environment, uptime: process.uptime(), cached: true, responseTime: "<1ms", }, ttl: 3600000, // 1 hour }, { key: "ultra:GET:/ping", value: { pong: true, timestamp: Date.now(), service: systemInfo.serviceName, cached: true, }, ttl: 3600000, }, { key: "ultra:GET:/status", value: { status: "healthy", timestamp: Date.now(), service: systemInfo.serviceName, version: systemInfo.version, cached: true, }, ttl: 3600000, }, ]; // Add custom health data if provided if (this.options.performance?.customHealthData) { try { const customHealthData = await this.options.performance.customHealthData(); ultraFastResponses[0].value = { ...ultraFastResponses[0].value, ...customHealthData, }; } catch (error) { Logger.logger.warn("cache", "Custom health data generation failed during warmup:", error.message); } } // Add custom status data if provided if (this.options.performance?.customStatusData) { try { const customStatusData = await this.options.performance.customStatusData(); ultraFastResponses[2].value = { ...ultraFastResponses[2].value, ...customStatusData, }; } catch (error) { Logger.logger.warn("cache", "Custom status data generation failed during warmup:", error.message); } } // Warm up cache asynchronously const warmupPromises = ultraFastResponses.map(async (item) => { try { await this.cache.set(item.key, item.value, { ttl: item.ttl, }); } catch (error) { Logger.logger.warn("cache", `Failed to warm up ${item.key}:`, error.message); } }); await Promise.all(warmupPromises); } catch (error) { Logger.logger.warn("cache", "Cache warmup failed:", error.message); } } /** * Generate ultra-fast cache key with minimal overhead */ generateUltraFastCacheKey(req) { return `ultra:${req.method}:${req.path}`; } /** * Generate standard cache key for request */ generateCacheKey(req, customKey) { if (typeof customKey === "function") { return customKey(req); } if (typeof customKey === "string") { return customKey; } // Auto-generate key based on route and params const baseKey = `${req.method}:${req.route?.path || req.path}`; const params = Object.keys(req.params || {}).length > 0 ? `:${JSON.stringify(req.params)}` : ""; const query = Object.keys(req.query || {}).length > 0 ? `:${JSON.stringify(req.query)}` : ""; return `${baseKey}${params}${query}`; } /** * Get cache TTL based on execution path */ getCacheTTLForPath(pathType) { switch (pathType) { case "ultra-fast": return 3600000; // 1 hour for ultra-fast responses case "fast": return 1800000; // 30 minutes for fast responses case "standard": return 300000; // 5 minutes for standard responses default: return 300000; } } /** * Warm up cache with provided data */ async warmUpCache(data) { await CacheFactory.CacheUtils.warmUp(this.cache, data); } /** * Get cache statistics */ async getCacheStats() { return await this.cache.getStats(); } /** * Get cache health status */ getCacheHealth() { return this.cache.getHealth(); } /** * Invalidate cache by pattern/tags */ async invalidateCache(pattern) { await this.cache.invalidateByTags([pattern]); } } exports.CacheManager = CacheManager; //# sourceMappingURL=CacheManager.js.map