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

View File

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

View File

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