UNPKG

hypershield

Version:

Middleware suite for high-performance and resilient APIs

124 lines 4.66 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheService = void 0; const constants_1 = require("../../../core/constants/constants"); const redis_1 = require("../../../shared/cache/redis"); const memory_1 = require("../../../shared/cache/memory"); class CacheService { constructor(config) { this.config = config; this.provider = this.initializeProvider(); } initializeProvider() { if (!this.config.enabled) { throw new Error('Cache is not enabled in configuration'); } switch (this.config.provider) { case 'memory': return new memory_1.MemoryCache(); case 'redis': if (!this.config.connection) { throw new Error('Redis connection configuration is required'); } const redisOptions = { host: this.config.connection.host || 'localhost', port: this.config.connection.port || 6379, password: this.config.connection.password || '' }; return new redis_1.RedisCache(redisOptions); default: throw new Error(`Unsupported cache provider: ${this.config.provider}`); } } get(key) { return __awaiter(this, void 0, void 0, function* () { try { const result = yield this.provider.get(key); return result === undefined ? null : result; } catch (error) { console.error(`Cache get error: ${error}`); return null; } }); } set(key, value, ttl) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (value === undefined || value === null) { return; } const validTtl = (_a = ttl !== null && ttl !== void 0 ? ttl : this.config.ttl) !== null && _a !== void 0 ? _a : constants_1.CACHE.DEFAULT_TTL; yield this.provider.set(key, value, validTtl); } catch (error) { console.error(`Cache set error: ${error}`); } }); } delete(key) { return __awaiter(this, void 0, void 0, function* () { try { yield this.provider.delete(key); } catch (error) { console.error(`Cache delete error: ${error}`); } }); } clear() { return __awaiter(this, void 0, void 0, function* () { try { yield this.provider.clear(); } catch (error) { console.error(`Cache clear error: ${error}`); } }); } has(key) { return __awaiter(this, void 0, void 0, function* () { try { const value = yield this.get(key); return value !== null; } catch (error) { console.error(`Cache check error: ${error}`); return false; } }); } getOrSet(key, factory, ttl) { return __awaiter(this, void 0, void 0, function* () { const cached = yield this.get(key); if (cached !== null) { return cached; } const value = yield factory(); yield this.set(key, value, ttl); return value; }); } mget(keys) { return __awaiter(this, void 0, void 0, function* () { return Promise.all(keys.map(key => this.get(key))); }); } mset(entries) { return __awaiter(this, void 0, void 0, function* () { yield Promise.all(entries.map(entry => this.set(entry.key, entry.value, entry.ttl))); }); } } exports.CacheService = CacheService; //# sourceMappingURL=cacheService.js.map