Created
May 4, 2024 21:27
-
-
Save rubinovitz/1d21a90d3ec837758f89605b3c4450b9 to your computer and use it in GitHub Desktop.
RedisClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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