adjusts in redis

This commit is contained in:
Davidson Gomes 2024-04-09 07:30:15 -03:00
parent 04575d8051
commit 3bc692d894
4 changed files with 37 additions and 28 deletions

View File

@ -43,73 +43,81 @@ export class RedisCache {
}
}
public async instanceKeys(): Promise<string[]> {
public async getInstanceKeys(): Promise<string[]> {
const keys: string[] = [];
try {
this.logger.verbose('Fetching instance keys');
for await (const key of this.client.scanIterator({ MATCH: `${this.redisEnv.PREFIX_KEY}:*` })) {
keys.push(key);
}
return keys;
} catch (error) {
this.logger.error('Error fetching instance keys ' + error);
throw error;
}
return keys;
}
public async keyExists(key?: string) {
if (key) {
this.logger.verbose('keyExists: ' + key);
return !!(await this.instanceKeys()).find((i) => i === key);
}
this.logger.verbose('keyExists: ' + this.instanceName);
return !!(await this.instanceKeys()).find((i) => i === this.instanceName);
}
public async writeData(field: string, data: any) {
try {
this.logger.verbose('writeData: ' + field);
const json = JSON.stringify(data, BufferJSON.replacer);
const keys = await this.getInstanceKeys();
const targetKey = key || this.instanceName;
this.logger.verbose('keyExists: ' + targetKey);
return keys.includes(targetKey);
} catch (error) {
return false;
}
}
return await this.client.hSet(this.redisEnv.PREFIX_KEY + ':' + this.instanceName, field, json);
public async setData(field: string, data: any) {
try {
this.logger.verbose('setData: ' + field);
const json = JSON.stringify(data, BufferJSON.replacer);
await this.client.hSet(`${this.redisEnv.PREFIX_KEY}-${this.instanceName}`, field, json);
return true;
} catch (error) {
this.logger.error(error);
return false;
}
}
public async readData(field: string) {
public async getData(field: string): Promise<any | null> {
try {
this.logger.verbose('readData: ' + field);
const data = await this.client.hGet(this.redisEnv.PREFIX_KEY + ':' + this.instanceName, field);
this.logger.verbose('getData: ' + field);
const data = await this.client.hGet(`${this.redisEnv.PREFIX_KEY}-${this.instanceName}`, field);
if (data) {
this.logger.verbose('readData: ' + field + ' success');
this.logger.verbose('getData: ' + field + ' success');
return JSON.parse(data, BufferJSON.reviver);
}
this.logger.verbose('readData: ' + field + ' not found');
this.logger.verbose('getData: ' + field + ' not found');
return null;
} catch (error) {
this.logger.error(error);
return null;
}
}
public async removeData(field: string) {
public async removeData(field: string): Promise<boolean> {
try {
this.logger.verbose('removeData: ' + field);
return await this.client.hDel(this.redisEnv.PREFIX_KEY + ':' + this.instanceName, field);
await this.client.hDel(`${this.redisEnv.PREFIX_KEY}-${this.instanceName}`, field);
return true;
} catch (error) {
this.logger.error(error);
return false;
}
}
public async delAll(hash?: string) {
public async delAll(hash?: string): Promise<boolean> {
try {
this.logger.verbose('instance delAll: ' + hash);
const result = await this.client.del(hash || this.redisEnv.PREFIX_KEY + ':' + this.instanceName);
return result;
const targetHash = hash || `${this.redisEnv.PREFIX_KEY}-${this.instanceName}`;
this.logger.verbose('instance delAll: ' + targetHash);
const result = await this.client.del(targetHash);
return !!result;
} catch (error) {
this.logger.error(error);
return false;
}
}
}

View File

@ -17,7 +17,7 @@ export async function useMultiFileAuthStateRedisDb(cache: RedisCache): Promise<{
const writeData = async (data: any, key: string): Promise<any> => {
try {
return await cache.writeData(key, data);
return await cache.setData(key, data);
} catch (error) {
return logger.error({ localError: 'writeData', error });
}
@ -25,9 +25,9 @@ export async function useMultiFileAuthStateRedisDb(cache: RedisCache): Promise<{
const readData = async (key: string): Promise<any> => {
try {
return await cache.readData(key);
return await cache.getData(key);
} catch (error) {
logger.error({ readData: 'writeData', error });
logger.error({ localError: 'readData', error });
return;
}
};

View File

@ -375,7 +375,7 @@ export class WAMonitoringService {
private async loadInstancesFromRedis() {
this.logger.verbose('Redis enabled');
await this.cache.connect(this.redis as Redis);
const keys = await this.cache.instanceKeys();
const keys = await this.cache.getInstanceKeys();
if (keys?.length > 0) {
this.logger.verbose('Reading instance keys and setting instances');

View File

@ -1032,7 +1032,8 @@ export class BaileysStartupService extends WAStartupService {
if (
(type !== 'notify' && type !== 'append') ||
received.message?.protocolMessage ||
received.message?.pollUpdateMessage
received.message?.pollUpdateMessage ||
!received?.message
) {
this.logger.verbose('message rejected');
return;