UNPKG

@sync-in/server

Version:

The secure, open-source platform for file storage, sharing, collaboration, and sync

53 lines (52 loc) 2.1 kB
/* * Copyright (C) 2012-2025 Johan Legrand <johan.legrand@sync-in.com> * This file is part of Sync-in | The open source file sync and share solution * See the LICENSE file for licensing details */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "CacheDecorator", { enumerable: true, get: function() { return CacheDecorator; } }); const _common = require("@nestjs/common"); const _cacheservice = require("./services/cache.service"); function CacheDecorator(TTL = 120, updateCache = false) { // if updateCache is true, we update the value in the cache on each call const injector = (0, _common.Inject)(_cacheservice.Cache); return (target, _key, descriptor)=>{ injector(target, 'cache'); const originalMethod = descriptor.value; const memoize = async function(...args) { try { const cacheKey = this.cache.genSlugKey(this.constructor.name, originalMethod.name, ...args); const cacheResult = await this.cache.get(cacheKey); if (cacheResult !== undefined) { if (updateCache) { originalMethod.apply(this, args).then((r)=>{ this.cache.set(cacheKey, r, TTL).catch((e)=>this.logger.error(`${memoize.name} - ${e}`)); }); } return cacheResult; } const r = await originalMethod.apply(this, args); this.cache.set(cacheKey, r, TTL).catch((e)=>this.logger.error(`${memoize.name} - ${e}`)); return r; } catch (e) { console.error(e); return originalMethod(args); } }; // keep the original function name Object.defineProperty(memoize, 'name', { value: originalMethod.name, writable: false }); // assign memoize function descriptor.value = memoize; }; } //# sourceMappingURL=cache.decorator.js.map