// Key Value, 23.08.15.20.29 // Implemented in `redis`. import { createClient } from "redis"; const redisConnectionString = process.env.REDIS_CONNECTION_STRING; const dev = process.env.NETLIFY_DEV; let client; async function connect() { if (client && client.isOpen) { console.log("🔵 Redis client is already connected."); return; } client = !dev ? createClient({ url: redisConnectionString }) : createClient(); client.on("error", (err) => console.log("🔴 Redis client error!", err)); await client.connect(); } async function disconnect() { if (!client?.isOpen) return; // Ensure the client is open before quitting await client.quit(); } async function set(collection, key, value) { await client.HSET(collection, key, value); } async function del(collection, key) { await client.HDEL(collection, key); } async function get(collection, key) { return await client.HGET(collection, key); } // Publish via redis. async function pub(channel, message) { try { await client.publish(channel, message); console.log("Published:", channel, message); } catch (err) { console.log("🙅‍♂️ Could not publish:", channel, err); } return true; } export { connect, get, set, del, pub, disconnect };