UNPKG

asksuite-core

Version:
53 lines (44 loc) 1.23 kB
class CacheRedis { constructor({ client, prefixKey, expirationTime, isJson, deserializeFunc }) { this.client = client; this.prefixKey = prefixKey; this.expirationTime = expirationTime; this.isJson = isJson; this.deserializeFunc = deserializeFunc; } formatKey(key) { if (this.prefixKey) { return `${this.prefixKey}:${key}`; } return key; } async deserialize(data) { if (this.deserializeFunc) { return this.deserializeFunc(data); } return data; } async remove(key) { const formattedKey = this.formatKey(key); const value = await this.client.remove(formattedKey); return this.deserialize(value); } async get(key) { const formattedKey = this.formatKey(key); const value = await this.client.getValue(formattedKey, this.isJson); return this.deserialize(value); } async set(key, value) { const formattedKey = this.formatKey(key); if (this.expirationTime) { return this.client.setValueWithExpirationTime( formattedKey, value, this.isJson, this.expirationTime, ); } return this.client.setValue(formattedKey, value, this.isJson); } } module.exports = { CacheRedis };