How to disconnect redis client before new client to be connected on Next API

64 Views Asked by At

I had initialised a client in this way, and everytime redisManager is called it creates a new client to be connected o the server.

import { createClient } from "redis"

const isConnected : {
    status?: boolean
} = {
};

export const client = createClient({
 url: `${process.env.REDIS_PUBLIC_URL}`
})

const redisManager = async () => {
    if(!isConnected.status) 
        try {
            await client.connect().then(() => isConnected.status = true);
            console.log('Redis Connected Successfully')
        } catch (error) {
            console.log("Error Connecting Redis\n", error);
            process.exit(1);
        }
    else 
        console.log("Redis already Connected!!")
}

export default redisManager;

UPDATE: on React side, using cleanup function to disconnect it rn, but maybe something from server-side can work?

as on every re-render, it creates new client and connects it. any possible ways to use the same?

0

There are 0 best solutions below