UNPKG

cache-manager-ioredis-store

Version:
116 lines 5.24 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.redisInsStore = exports.redisStore = void 0; const ioredis_1 = __importDefault(require("ioredis")); const getVal = (value) => JSON.stringify(value) || '"undefined"'; function builder(redisCache, reset, keys, options) { const isCacheable = (options === null || options === void 0 ? void 0 : options.isCacheable) || ((value) => value !== undefined && value !== null); const throwNoncacheable = (options === null || options === void 0 ? void 0 : options.throwNoncacheable) === undefined ? true : options.throwNoncacheable; return { get(key) { return __awaiter(this, void 0, void 0, function* () { const val = yield redisCache.get(key); if (val === undefined || val === null) return undefined; else return JSON.parse(val); }); }, set(key, value, ttl) { return __awaiter(this, void 0, void 0, function* () { if (!isCacheable(value)) { if (throwNoncacheable) throw new Error(`"${value}" is not a cacheable value`); else return; } const t = ttl === undefined ? options === null || options === void 0 ? void 0 : options.ttl : ttl; if (t) yield redisCache.setex(key, t, getVal(value)); else yield redisCache.set(key, getVal(value)); }); }, mset(args, ttl) { return __awaiter(this, void 0, void 0, function* () { const t = ttl === undefined ? options === null || options === void 0 ? void 0 : options.ttl : ttl; if (t) { const multi = redisCache.multi(); for (const [key, value] of args) { if (!isCacheable(value)) { if (throwNoncacheable) throw new Error(`"${getVal(value)}" is not a cacheable value`); else continue; } multi.setex(key, t / 1000, getVal(value)); } yield multi.exec(); } else yield redisCache.mset(args.flatMap(([key, value]) => { if (!isCacheable(value)) { if (throwNoncacheable) throw new Error(`"${getVal(value)}" is not a cacheable value`); else return []; } return [key, getVal(value)]; })); }); }, mget: (...args) => redisCache .mget(args) .then((x) => x.map((x) => x === null || x === undefined ? undefined : JSON.parse(x))), mdel(...args) { return __awaiter(this, void 0, void 0, function* () { yield redisCache.del(args); }); }, del(key) { return __awaiter(this, void 0, void 0, function* () { yield redisCache.del(key); }); }, ttl: (key) => __awaiter(this, void 0, void 0, function* () { return redisCache.ttl(key); }), keys: (pattern = '*') => keys(pattern), reset, isCacheable, get client() { return redisCache; }, }; } function redisStore(options) { return __awaiter(this, void 0, void 0, function* () { options || (options = {}); const redisCache = 'clusterConfig' in options ? new ioredis_1.default.Cluster(options.clusterConfig.nodes, options.clusterConfig.options) : new ioredis_1.default(options); return redisInsStore(redisCache, options); }); } exports.redisStore = redisStore; function redisInsStore(redisCache, options) { const reset = () => __awaiter(this, void 0, void 0, function* () { yield redisCache.flushall(); }); const keys = (pattern) => redisCache.keys(pattern); return builder(redisCache, reset, keys, options); } exports.redisInsStore = redisInsStore; //# sourceMappingURL=index.js.map