Team Ai
Apppublic

oex2003/evolution-api

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
rediscache.client.ts57 linesDownload Raw Back to cache
1import { CacheConf, CacheConfRedis, configService } from '@config/env.config';2import { Logger } from '@config/logger.config';3import { createClient, RedisClientType } from 'redis';4 5class Redis {6  private logger = new Logger('Redis');7  private client: RedisClientType = null;8  private conf: CacheConfRedis;9  private connected = false;10 11  constructor() {12    this.conf = configService.get<CacheConf>('CACHE')?.REDIS;13  }14 15  getConnection(): RedisClientType {16    if (this.connected) {17      return this.client;18    } else {19      this.client = createClient({20        url: this.conf.URI,21      });22 23      this.client.on('connect', () => {24        this.logger.verbose('redis connecting');25      });26 27      this.client.on('ready', () => {28        this.logger.verbose('redis ready');29        this.connected = true;30      });31 32      this.client.on('error', () => {33        this.logger.error('redis disconnected');34        this.connected = false;35      });36 37      this.client.on('end', () => {38        this.logger.verbose('redis connection ended');39        this.connected = false;40      });41 42      try {43        this.client.connect();44        this.connected = true;45      } catch (e) {46        this.connected = false;47        this.logger.error('redis connect exception caught: ' + e);48        return null;49      }50 51      return this.client;52    }53  }54}55 56export const redisClient = new Redis();57