UNPKG

@venix/cachified

Version:

A plug and play NestJS redis cache for intensive functions.

107 lines 6.01 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; 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.CachifiedService = void 0; const common_1 = require("@nestjs/common"); const nestjs_discovery_1 = require("@golevelup/nestjs-discovery"); const IORedis = require("ioredis"); const util_1 = require("../util"); let CachifiedService = class CachifiedService { constructor(options, discoveryService) { this.options = options; this.discoveryService = discoveryService; } onModuleInit() { return __awaiter(this, void 0, void 0, function* () { this.redisClient = this.options.client instanceof IORedis ? this.options.client : new IORedis(this.options.client); const cachedMethods = yield this.discoveryService.providerMethodsWithMetaAtKey(util_1.CACHIFIED_DECORATOR); for (const cachedMethod of cachedMethods) { const { parentClass, methodName, handler } = cachedMethod.discoveredMethod; const { meta: options } = cachedMethod; if (!this.options.enabled) return; const cachedFunction = (...args) => __awaiter(this, void 0, void 0, function* () { args = options.transform ? options.transform(...args) : args; const invalidArgs = args.filter(arg => typeof arg === 'object'); if (invalidArgs.length) throw new Error(`Invalid arguments (typeof object) specified to Cachified function: ${args}`); const redisKey = `cachified:${parentClass.name}.${methodName}(${args.join(',')})`; const val = yield this.getRawVal(redisKey); if (val !== null) { try { return JSON.parse(val); } catch (_a) { return val; } } const returnedData = yield handler.apply(parentClass.instance, args); try { yield this.setRawValWithExpiry(redisKey, JSON.stringify(returnedData), options.expirySeconds); } catch (_b) { yield this.setRawValWithExpiry(redisKey, returnedData, options.expirySeconds); } return returnedData; }); cachedMethod.discoveredMethod.parentClass.instance[cachedMethod.discoveredMethod.methodName] = cachedFunction.bind(this); } }); } getRawVal(key) { return __awaiter(this, void 0, void 0, function* () { if (!this.redisClient) throw new Error('Attempted Cachified usage without initialization'); return yield this.redisClient.get(key); }); } setRawValWithExpiry(key, value, expiry) { return __awaiter(this, void 0, void 0, function* () { if (!this.redisClient) throw new Error('Attempted Cachified usage without initialization'); return yield this.redisClient.set(key, value, 'EX', expiry); }); } invalidate(name, ...args) { return __awaiter(this, void 0, void 0, function* () { const invalidArgs = args.filter(arg => typeof arg === 'object'); if (invalidArgs.length) throw new Error(`Invalid arguments (typeof object) specified to Cachified function: ${args}`); const redisKey = `cachified:${name}(${args.join(',')})`; return yield this.redisClient.del(redisKey); }); } invalidateAll(name) { return __awaiter(this, void 0, void 0, function* () { const keys = yield this.redisClient.keys(`${this.redisClient.options.keyPrefix || ''}cachified:${name}(*)`); return yield this.redisClient.del(...keys.map(key => key.slice((this.redisClient.options.keyPrefix || '').length))); }); } }; CachifiedService = __decorate([ common_1.Injectable(), __param(0, common_1.Inject(util_1.CACHIFIED_OPTIONS)), __metadata("design:paramtypes", [Object, nestjs_discovery_1.DiscoveryService]) ], CachifiedService); exports.CachifiedService = CachifiedService; //# sourceMappingURL=cachified.service.js.map