cache-manager-ioredis-wildcard-delete
Version:
Redis store for node-cache-manager with wildcard deletion
133 lines • 5.54 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.redisInsStore = exports.redisStore = exports.avoidNoCacheable = exports.NoCacheableError = void 0;
const ioredis_1 = __importDefault(require("ioredis"));
const getVal = (value) => JSON.stringify(value) || '"undefined"';
class NoCacheableError {
constructor(message) {
this.message = message;
this.name = 'NoCacheableError';
}
}
exports.NoCacheableError = NoCacheableError;
const avoidNoCacheable = (p) => __awaiter(void 0, void 0, void 0, function* () {
try {
return yield p;
}
catch (e) {
if (!(e instanceof NoCacheableError))
throw e;
}
});
exports.avoidNoCacheable = avoidNoCacheable;
function batchDeletionKeysByPattern(redis, key) {
return __awaiter(this, void 0, void 0, function* () {
const stream = redis.scanStream({
match: key,
});
stream.on('data', function (resultKeys) {
if (resultKeys.length) {
redis.unlink(resultKeys);
}
});
});
}
function builder(redisCache, reset, keys, options) {
const isCacheable = (options === null || options === void 0 ? void 0 : options.isCacheable) || ((value) => value !== undefined && value !== null);
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))
throw new NoCacheableError(`"${value}" is not a cacheable value`);
const t = ttl === undefined ? options === null || options === void 0 ? void 0 : options.ttl : ttl;
if (t !== undefined && t !== 0)
yield redisCache.set(key, getVal(value), 'PX', t);
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 !== undefined && t !== 0) {
const multi = redisCache.multi();
for (const [key, value] of args) {
if (!isCacheable(value))
throw new NoCacheableError(`"${getVal(value)}" is not a cacheable value`);
multi.set(key, getVal(value), 'PX', t);
}
yield multi.exec();
}
else
yield redisCache.mset(args.flatMap(([key, value]) => {
if (!isCacheable(value))
throw new Error(`"${getVal(value)}" is not a cacheable value`);
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* () {
if (key.includes('*')) {
yield batchDeletionKeysByPattern(redisCache, key);
}
yield redisCache.del(key);
});
},
ttl: (key) => __awaiter(this, void 0, void 0, function* () { return redisCache.pttl(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 = 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.flushdb();
});
const keys = (pattern) => redisCache.keys(pattern);
return builder(redisCache, reset, keys, options);
}
exports.redisInsStore = redisInsStore;
//# sourceMappingURL=index.js.map