Last active
June 20, 2022 14:07
-
-
Save trnktms/a494e5d323614d257903996d85fb43ed to your computer and use it in GitHub Desktop.
This file contains 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 } 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