Skip to content

Instantly share code, notes, and snippets.

@rubinovitz
Created May 4, 2024 21:27
Show Gist options
  • Save rubinovitz/1d21a90d3ec837758f89605b3c4450b9 to your computer and use it in GitHub Desktop.
Save rubinovitz/1d21a90d3ec837758f89605b3c4450b9 to your computer and use it in GitHub Desktop.
RedisClient
import { createClient, RedisClientType } from "redis";
// Define a variable to hold the client instance
let client: RedisClientType | null = null;
export async function getRedisClient() {
if (client) {
// Return the existing client if it's already initialized
return client;
}
// Create a new client instance if it's the first time this function is called
client = createClient({ url: process.env.REDIS_URL || "" });
client.on("error", (err) => console.log("Redis Client Error", err));
// Connect to the Redis server
await client.connect();
return client;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment