Redis connect and disconnect methods

JS
S
JavaScript

Sample snippet that shows the connect and disconnect operations for redis. For use with: https://www.npmjs.com/package/redis

1  public connectToRedis(): Promise<any> {
2    console.log('connectToRedis', process.env.REDIS_ADDRESS);
3    return new Promise((resolve, reject) => {
4      this.redisConnection = redis.createClient(6379, process.env.REDIS_ADDRESS);
5      this.redisConnection.on('error', (err: any) => {
6        console.log('redis error', err);
7        if (err) { throw err; }
8      });
9      this.redisConnection.on('ready', (status: string) => {
10        console.log(`Redis Cache redisConnection Ready`, process.env.REDIS_ADDRESS);
11        resolve(true);
12      });
13    });
14  }
15
16  public disconnectFromRedis(): void {
17    this.redisConnection.quit(); // sends the quit command to the redis server and ends cleanly right after all running commands were properly handled
18  }

Created on 2/8/2019