UNPKG

ltijs

Version:

Easily turn your web application into a LTI 1.3 Learning Tool.

59 lines • 2.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisCacheManager = void 0; const ioredis_1 = __importDefault(require("ioredis")); const errors_1 = require("#services/cache-manager/redis/errors"); /** * A `CacheManager` backed by Redis -- the opt-in choice for real, cross-instance-coordinated caching * (the default `MockCacheManager` is a no-op, since a per-process cache can't stay consistent across * multiple ltijs instances behind a load balancer). Not constructed by `Provider` automatically; import * it, construct it, and pass it as `ProviderOptions.cacheManager`, the same way `MongoLegacyDatabaseManager` * is opted into. */ class RedisCacheManager { LOG_COMPONENT = 'redisCacheManager'; PX_OPTION = 'PX'; client; logger; /** Throws `MissingCacheConfigError` if `config` is missing or `config.url` is empty. */ constructor(logger, config) { if (config === undefined || config.url === '') throw new errors_1.MissingCacheConfigError(); // `lazyConnect` mirrors DatabaseManager's listen()-gated connection lifecycle -- the client doesn't // connect on construction, only once listen() explicitly does so. this.client = new ioredis_1.default(config.url, { lazyConnect: true, ...config.connection }); this.logger = logger; } async listen() { await this.client.connect(); this.logger.debug(this.LOG_COMPONENT, 'Redis connected'); } async close() { await this.client.quit(); } async get(key) { const raw = await this.client.get(key); if (raw === null) return undefined; try { return JSON.parse(raw); } catch { // Corrupted or foreign (non-JSON) data under this key -- treated as a miss, not an error, // consistent with the CacheManager interface's "a miss is always a safe outcome" contract. return undefined; } } async set(key, value, ttlMs) { await this.client.set(key, JSON.stringify(value), this.PX_OPTION, ttlMs); } async delete(key) { await this.client.del(key); } } exports.RedisCacheManager = RedisCacheManager; exports.default = RedisCacheManager; //# sourceMappingURL=redis-cache-manager.service.js.map