Skip to content

Instantly share code, notes, and snippets.

@trnktms
Last active Jun 20, 2022
Embed
What would you like to do?
import { createClient } from 'redis';
class RedisClient {
private static redisClient: ReturnType<typeof createClient>;
private static initRedisClient() {
const redisClient = createClient({
url: process.env.REDIS_URL,
password: process.env.REDIS_PASSWORD,
socket: {
tls: !process.env.REDIS_URL?.includes?.('localhost'),
connectTimeout: 3000,
reconnectStrategy: () => 360000,
},
});
redisClient.on('connect', () => console.log('Redis client is connected...'));
redisClient.on('ready', () => console.log('Redis client is ready...'));
redisClient.on('error', (err) => console.log('Redis client error occured', err));
redisClient.on('end', () => console.log('Redis client is disconnected'));
process.on('SIGINT', () => redisClient.quit());
redisClient.connect();
return redisClient;
}
public static getRedisClient(): ReturnType<typeof createClient> {
if (!RedisClient.redisClient) {
RedisClient.redisClient = RedisClient.initRedisClient();
}
return RedisClient.redisClient;
}
}
export default RedisClient;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment