nestjs-request-deduplication
Version:
[](https://www.npmjs.com/package/nestjs-request-deduplication) [](https://gith
44 lines (43 loc) • 1.47 kB
JavaScript
import { __awaiter } from "tslib";
import Keyv from 'keyv';
import KeyvMemcache from '@keyv/memcache';
import { Logger } from '@nestjs/common';
export class MemcachedAdapter {
constructor(options) {
this.options = options;
this.logger = new Logger(MemcachedAdapter.name);
}
init() {
return __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 KeyvMemcache(uri, this.options.memcachedConfig);
this.keyv = new Keyv({
store: memcache,
namespace: 'request-deduplication',
});
this.logger.log('Memcached storage adapter initialized successfully');
});
}
get(key) {
return __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 __awaiter(this, void 0, void 0, function* () {
yield this.keyv.set(key, value, ttl);
});
}
delete(key) {
return __awaiter(this, void 0, void 0, function* () {
yield this.keyv.delete(key);
});
}
}