UNPKG

nestjs-request-deduplication

Version:

[![npm version](https://badge.fury.io/js/nestjs-request-deduplication.svg)](https://www.npmjs.com/package/nestjs-request-deduplication) [![CI](https://github.com/daniyel/nestjs-request-deduplication/actions/workflows/pr-checks.yml/badge.svg)](https://gith

48 lines (47 loc) 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MemcachedAdapter = void 0; const tslib_1 = require("tslib"); const keyv_1 = tslib_1.__importDefault(require("keyv")); const memcache_1 = tslib_1.__importDefault(require("@keyv/memcache")); const common_1 = require("@nestjs/common"); class MemcachedAdapter { constructor(options) { this.options = options; this.logger = new common_1.Logger(MemcachedAdapter.name); } init() { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (!this.options.memcachedConfig) { throw new Error('Memcached configuration is required'); } const uri = this.options.memcachedConfig.uri; const memcache = new memcache_1.default(uri, this.options.memcachedConfig); this.keyv = new keyv_1.default({ store: memcache, namespace: 'request-deduplication', }); this.logger.log('Memcached storage adapter initialized successfully'); }); } get(key) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const value = yield this.keyv.get(key); if (value === undefined) { throw new Error(`Key ${key} not found`); } return value; }); } set(key, value, ttl) { return tslib_1.__awaiter(this, void 0, void 0, function* () { yield this.keyv.set(key, value, ttl); }); } delete(key) { return tslib_1.__awaiter(this, void 0, void 0, function* () { yield this.keyv.delete(key); }); } } exports.MemcachedAdapter = MemcachedAdapter;