cache-manager-redis-yet
Version:
Redis store for node-cache-manager updated
164 lines (163 loc) • 6.53 kB
JavaScript
;
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.redisClusterInsStore = exports.redisClusterStore = exports.redisInsStore = exports.redisStore = exports.builder = void 0;
const node_util_1 = require("node:util");
const redis_1 = require("redis");
require("@redis/client");
require("@redis/bloom");
require("@redis/graph");
require("@redis/json");
require("@redis/search");
require("@redis/time-series");
const getVal = (value) => JSON.stringify(value) || '"undefined"';
function builder(redisCache, name, reset, keys, options) {
const isCacheableValue = (options === null || options === void 0 ? void 0 : options.isCacheableValue) ||
((value) => value !== undefined && value !== null);
const get = (key) => __awaiter(this, void 0, void 0, function* () {
const val = yield redisCache.get(key);
if (val)
return JSON.parse(val);
else
return null;
});
const set = (key, value, ttl) => __awaiter(this, void 0, void 0, function* () {
if (!isCacheableValue(value))
throw new Error(`"${value}" is not a cacheable value`);
if (typeof ttl === 'object') {
if (typeof ttl.ttl === 'function')
ttl = ttl.ttl(value);
else
ttl = ttl.ttl;
}
if (ttl)
yield redisCache.setEx(key, ttl, getVal(value));
else
yield redisCache.set(key, getVal(value));
});
const mset = (args, ttl) => __awaiter(this, void 0, void 0, function* () {
if (!ttl)
ttl = options === null || options === void 0 ? void 0 : options.ttl;
if (ttl) {
const multi = redisCache.multi();
for (const [key, value] of args) {
if (!isCacheableValue(value))
throw new Error(`"${getVal(value)}" is not a cacheable value`);
multi.setEx(key, ttl, getVal(value));
}
yield multi.exec();
}
else
yield redisCache.mSet(args.map(([key, value]) => {
if (!isCacheableValue(value))
throw new Error(`"${getVal(value)}" is not a cacheable value`);
return [key, getVal(value)];
}));
});
const mget = (...args) => redisCache
.mGet(args)
.then((x) => x.map((x) => (x === null ? null : JSON.parse(x))));
return {
isCacheableValue,
set(key, value, ttl, cb) {
if (typeof cb === 'function')
(0, node_util_1.callbackify)((set))(key, value, ttl, cb);
else
return set(key, value, ttl);
},
get(key, opt, cb) {
if (typeof cb === 'function')
(0, node_util_1.callbackify)((get))(key, cb);
else
return get(key);
},
mset(args, ttl, cb) {
if (typeof cb === 'function')
(0, node_util_1.callbackify)(mset)(args, ttl, cb);
else
return mset(args, ttl);
},
mget(...args) {
if (typeof args.at(-1) === 'function') {
const cb = args.pop();
(0, node_util_1.callbackify)(() => mget(...args))(cb);
}
else
return mget(...args);
},
del(args, cb) {
const fn = () => __awaiter(this, void 0, void 0, function* () {
yield redisCache.del(args);
});
if (typeof cb === 'function')
(0, node_util_1.callbackify)(fn)(cb);
else
return fn();
},
ttl: (key, cb) => {
if (typeof cb === 'function')
(0, node_util_1.callbackify)(() => redisCache.ttl(key))(cb);
else
return redisCache.ttl(key);
},
name,
get getClient() {
return redisCache;
},
reset(cb) {
if (typeof cb === 'function')
(0, node_util_1.callbackify)(reset)(cb);
else
return reset();
},
keys(pattern = '*', cb) {
if (typeof cb === 'function')
(0, node_util_1.callbackify)(() => keys(pattern))(cb);
else
return keys(pattern);
},
};
}
exports.builder = builder;
function redisStore(options) {
return __awaiter(this, void 0, void 0, function* () {
const redisCache = (0, redis_1.createClient)(options);
yield redisCache.connect();
return redisInsStore(redisCache, options);
});
}
exports.redisStore = redisStore;
function redisInsStore(redisCache, options) {
const reset = () => __awaiter(this, void 0, void 0, function* () {
yield redisCache.flushDb();
});
const keys = (pattern) => redisCache.keys(pattern);
return builder(redisCache, 'redis', reset, keys, options);
}
exports.redisInsStore = redisInsStore;
function redisClusterStore(options) {
return __awaiter(this, void 0, void 0, function* () {
const redisCache = (0, redis_1.createCluster)(options);
yield redisCache.connect();
return redisClusterInsStore(redisCache, options);
});
}
exports.redisClusterStore = redisClusterStore;
function redisClusterInsStore(redisCache, options) {
const reset = () => __awaiter(this, void 0, void 0, function* () {
yield Promise.all(redisCache.getMasters().map((node) => node.client.flushDb()));
});
const keys = (pattern) => __awaiter(this, void 0, void 0, function* () {
return (yield Promise.all(redisCache.getMasters().map((node) => node.client.keys(pattern)))).flat();
});
return builder(redisCache, 'redis-cluster', reset, keys, options);
}
exports.redisClusterInsStore = redisClusterInsStore;