UNPKG

@gramio/storage-redis

Version:

Redis Storage for GramIO

31 lines (28 loc) 793 B
import { RedisClient } from "bun"; function redisStorage(optionsRaw = {}) { const isInstance = optionsRaw instanceof RedisClient; const options = isInstance ? {} : optionsRaw; const storage = isInstance ? optionsRaw : new RedisClient(options.url, options); return { async get(key) { const data = await storage.get(String(key)); return data ? JSON.parse(data) : void 0; }, async has(key) { return !!(await storage.get(String(key))); }, async set(key, value) { const data = JSON.stringify(value); if (options.$ttl) await storage.set(String(key), data, "EX", options.$ttl); else await storage.set(String(key), data); }, async delete(key) { const result = await storage.del(String(key)); return result === 1; }, }; } export { redisStorage };