mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-19 20:02:20 -06:00
fix: Improved how Redis works for instances
This commit is contained in:
@@ -4,16 +4,29 @@ import { BufferJSON } from '@whiskeysockets/baileys';
|
||||
import { Redis } from '../config/env.config';
|
||||
|
||||
export class RedisCache {
|
||||
constructor(private readonly redisEnv: Partial<Redis>, private instanceName?: string) {
|
||||
this.client = createClient({ url: this.redisEnv.URI });
|
||||
|
||||
this.client.connect();
|
||||
constructor() {
|
||||
process.on('beforeExit', async () => {
|
||||
if (this.statusConnection) {
|
||||
await this.client.disconnect();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private statusConnection = false;
|
||||
private instanceName: string;
|
||||
private redisEnv: Redis;
|
||||
|
||||
public set reference(reference: string) {
|
||||
this.instanceName = reference;
|
||||
}
|
||||
|
||||
public async connect(redisEnv: Redis) {
|
||||
this.client = createClient({ url: redisEnv.URI });
|
||||
await this.client.connect();
|
||||
this.statusConnection = true;
|
||||
this.redisEnv = redisEnv;
|
||||
}
|
||||
|
||||
private readonly logger = new Logger(RedisCache.name);
|
||||
private client: RedisClientType;
|
||||
|
||||
@@ -35,6 +48,7 @@ export class RedisCache {
|
||||
public async writeData(field: string, data: any) {
|
||||
try {
|
||||
const json = JSON.stringify(data, BufferJSON.replacer);
|
||||
|
||||
return await this.client.hSet(
|
||||
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||
field,
|
||||
@@ -51,6 +65,7 @@ export class RedisCache {
|
||||
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||
field,
|
||||
);
|
||||
|
||||
if (data) {
|
||||
return JSON.parse(data, BufferJSON.reviver);
|
||||
}
|
||||
@@ -79,20 +94,4 @@ export class RedisCache {
|
||||
this.logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async closeConnection() {
|
||||
try {
|
||||
await this.client.quit();
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async destructor() {
|
||||
await this.closeConnection();
|
||||
}
|
||||
|
||||
public async destroy() {
|
||||
await this.destructor();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user