cache-layer
Version:
An abstraction for cache providers, it exposes a common interface for diferente ways of cache
53 lines • 1.72 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ioredis_1 = __importDefault(require("ioredis"));
const ms_1 = __importDefault(require("ms"));
const OK = 'OK';
const NOT_OK = 'NOT_OK';
class RedisProvider {
constructor(options) {
if (options.ttl) {
const ttlNormalize = this.getTTL(options.ttl);
this.defaultTTL = ttlNormalize;
options.ttl = ttlNormalize;
}
this.client = new ioredis_1.default(options.port, options.host, options);
}
async get(key) {
const value = await this.client.get(key);
return value ? JSON.parse(value) : null;
}
async has(key) {
return this.client.exists(key);
}
async delete(key) {
const deleted = await this.client.del(key);
return deleted > 0;
}
async add(key, data, ttl) {
const expireAt = this.getTTL(ttl) || this.defaultTTL;
let saved = NOT_OK;
if (expireAt) {
saved = await this.client.setex(`${key}`, this.getTTL(ttl) || this.defaultTTL, JSON.stringify(data || {}));
}
else {
saved = await this.client.set(`${key}`, JSON.stringify(data || {}));
}
return saved === OK;
}
getTTL(ttl) {
let seconds;
if (typeof ttl === 'string') {
seconds = ms_1.default(ttl) / 1000;
}
else {
seconds = ttl / 1000;
}
return seconds <= 1 ? 1 : seconds;
}
}
exports.default = RedisProvider;
//# sourceMappingURL=redis-provider.js.map
;