UNPKG

nestjs-cache-dependency

Version:
155 lines 6.41 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); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheDependencyService = void 0; const common_1 = require("@nestjs/common"); const cache_manager_1 = require("@nestjs/cache-manager"); const isEqual = require("lodash.isequal"); const shared_utils_1 = require("@nestjs/common/utils/shared.utils"); let CacheDependencyService = class CacheDependencyService { constructor(cacheManager) { this.cacheManager = cacheManager; this._mergeDependencyVersions = function (arr1, arr2) { if (Array.isArray(arr2) && arr2.length > 0) { arr2.forEach(e2 => { let index = arr1.findIndex(e1 => { return e1.key == e2.key; }); if (index != -1) { arr1[index] = e2; } else { arr1.push(e2); } }); } return arr1; }; } async set(key, value, ttl, dependencyKeys) { const cacheKey = this._buildDataCacheKey(key); if ((0, shared_utils_1.isNil)(dependencyKeys)) { dependencyKeys = []; } else if (!Array.isArray(dependencyKeys)) { dependencyKeys = [dependencyKeys]; } dependencyKeys = dependencyKeys.map(dependency => { return this._buildDependencyCacheKey(dependency); }); const dependencies = await this._generateDependencyVersions(dependencyKeys); return await this.cacheManager.set(cacheKey, JSON.stringify([value, dependencies]), !(0, shared_utils_1.isNil)(ttl) ? ttl : undefined); } async get(key) { const cacheKey = this._buildDataCacheKey(key); const cachedData = await this.cacheManager.get(cacheKey); if (cachedData !== null) { const data = JSON.parse(cachedData); const isValid = await this._validateDependencyVersions(data); if (isValid) { return data[0]; } } return null; } async delete(key) { const cacheKey = this._buildDataCacheKey(key); return await this.cacheManager.del(cacheKey); } async invalidate(dependencyKeys) { if (dependencyKeys) { if (!Array.isArray(dependencyKeys)) { dependencyKeys = [dependencyKeys]; } dependencyKeys = dependencyKeys.map(e => { return this._buildDependencyCacheKey(e); }); await this._updateDependencyVersions(dependencyKeys); return true; } return false; } async _generateDependencyVersions(dependencyKeys) { let currentDependencies = await this._getDependencyVersions(dependencyKeys); const newDependencyKeys = []; for (const d of currentDependencies) { if (d.version === undefined) { newDependencyKeys.push(d.key); } } if (newDependencyKeys.length > 0) { const newDependencies = await this._updateDependencyVersions(newDependencyKeys); currentDependencies = this._mergeDependencyVersions(currentDependencies, newDependencies); } return currentDependencies; } async _getDependencyVersions(dependencyKeys) { const itemObjects = []; if (Array.isArray(dependencyKeys) && dependencyKeys.length > 0) { for (const e of dependencyKeys) { itemObjects.push({ key: e, version: await this.cacheManager.get(e), }); } } return itemObjects; } async _updateDependencyVersions(dependencyKeys) { const itemObjects = []; const version = String(Date.now()); if (Array.isArray(dependencyKeys) && dependencyKeys.length > 0) { for (let i = 0; i < dependencyKeys.length; i++) { await this.cacheManager.set(dependencyKeys[i], version, 0); itemObjects.push({ key: dependencyKeys[i], version: version }); } } return itemObjects; } async _validateDependencyVersions(cachedData) { let isValid = false; if (cachedData !== null && Array.isArray(cachedData) && cachedData.length === 2) { if (cachedData[1] === null || (Array.isArray(cachedData[1]) && cachedData[1].length == 0)) { return true; } else { const dependencyKeys = cachedData[1].map(e => e.key); const dependencies = await this._getDependencyVersions(dependencyKeys); if (isEqual(dependencies, cachedData[1])) { isValid = true; } } } return isValid; } _buildCacheKey(key, prefix) { return prefix + key; } _buildDataCacheKey(key) { return this._buildCacheKey(key, 'D_'); } _buildDependencyCacheKey(key) { return this._buildCacheKey(key, 'T_'); } }; exports.CacheDependencyService = CacheDependencyService; exports.CacheDependencyService = CacheDependencyService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)), __metadata("design:paramtypes", [Object]) ], CacheDependencyService); //# sourceMappingURL=cache-dependency.service.js.map