UNPKG

@sync-in/server

Version:

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

129 lines (128 loc) 4.75 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, "RedisCacheAdapter", { enumerable: true, get: function() { return RedisCacheAdapter; } }); const _common = require("@nestjs/common"); const _redis = require("redis"); const _shared = require("../../../common/shared"); const _configenvironment = require("../../../configuration/config.environment"); const _cacheservice = require("../services/cache.service"); function _ts_decorate(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; } function _ts_metadata(k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); } let RedisCacheAdapter = class RedisCacheAdapter { async has(key) { return await this.client.exists(key) === 1; } async keys(pattern) { const matches = []; for await (const key of this.client.scanIterator({ MATCH: pattern, COUNT: 100 })){ matches.push(key); } return matches; } async get(key) { return this.deserialize(await this.client.get(key)); } async mget(keys) { return (await this.client.mGet(keys)).map((v)=>this.deserialize(v)); } async set(key, data, ttl) { const exp = this.getTTL(ttl); return await this.client.set(key, this.serialize(data), { EX: exp === -1 ? undefined : exp }) === 'OK'; } async del(key) { return await this.client.unlink(key) > 0; } async mdel(keys) { const multi = this.client.multi(); for (const key of keys){ multi.unlink(key); } const results = await multi.exec(); return results.indexOf(1) > -1; } genSlugKey(...args) { return (0, _shared.createSlug)(args.join(' ')); } async quit() { await this.client.disconnect(); this.logger.verbose(`${this.quit.name}`); } getTTL(ttl) { /* ttl (seconds): - 0 : infinite expiration - undefined : default ttl */ return ttl ? ttl : ttl === 0 ? this.infiniteExpiration : this.defaultTTL; } serialize(data) { if (data === undefined || data === null) { return 'null'; } return JSON.stringify(data); } deserialize(data) { if (data === null) { return undefined; } return JSON.parse(data); } constructor(){ this.defaultTTL = _configenvironment.configuration.cache.ttl; this.infiniteExpiration = -1; this.logger = new _common.Logger(_cacheservice.Cache.name.toUpperCase()); this.reconnectOptions = { maxAttempts: 3, minConnectDelay: 6000, maxConnectDelay: 30000 }; this.reconnectStrategy = (attempts)=>{ if (attempts > this.reconnectOptions.maxAttempts) { this.logger.error('Too many retries on Redis server. Exiting'); process.exit(); } else { const wait = Math.min(this.reconnectOptions.minConnectDelay * Math.pow(2, attempts), this.reconnectOptions.maxConnectDelay); this.logger.warn(`Retrying connection to Redis server in ${wait / 1000}s`); return wait; } }; this.client = (0, _redis.createClient)({ url: _configenvironment.configuration.cache.redis, socket: { noDelay: true, reconnectStrategy: this.reconnectStrategy } }); this.client.connect().then(()=>{ this.client.on('error', (e)=>this.logger.error(e.message || e)); this.client.on('ready', ()=>this.logger.log(`Connected to Redis Server at ${this.client.options.url}`)); }); } }; RedisCacheAdapter = _ts_decorate([ (0, _common.Injectable)(), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", []) ], RedisCacheAdapter); //# sourceMappingURL=redis-cache.adapter.js.map