UNPKG

@amirmarmul/waba-common

Version:

![GitHub release](https://img.shields.io/github/v/release/amirmarmul/waba-common?style=flat-square)

45 lines (44 loc) 1.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisStore = void 0; class RedisStore { redis; prefix; constructor(redis, prefix = '') { this.redis = redis; this.prefix = prefix; } async get(key) { try { const value = await this.redis.get(key); if (value) { return JSON.parse(value); } } catch (error) { await this.forget(key); } return null; } async put(key, value, ttl) { return !!await this.redis.set(key, JSON.stringify(value), 'EX', ttl); } async forever(key, value) { return !!await this.redis.set(key, JSON.stringify(value)); } async forget(key) { const keys = await this.redis.keys(key); const pipeline = this.redis.pipeline(); keys.forEach((key) => { pipeline.del(key); }); return !!await pipeline.exec(); } async flush() { return await this.forget('*'); } getPrefix() { return this.prefix; } } exports.RedisStore = RedisStore;