mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 04:02:54 -06:00
Added redis cache for on whatsapp & TTL on .env
This commit is contained in:
parent
ce6438b9a8
commit
7a76600cd6
@ -37,6 +37,7 @@ DATABASE_SAVE_DATA_CHATS=true
|
|||||||
DATABASE_SAVE_DATA_LABELS=true
|
DATABASE_SAVE_DATA_LABELS=true
|
||||||
DATABASE_SAVE_DATA_HISTORIC=true
|
DATABASE_SAVE_DATA_HISTORIC=true
|
||||||
DATABASE_SAVE_IS_ON_WHATSAPP=true
|
DATABASE_SAVE_IS_ON_WHATSAPP=true
|
||||||
|
DATABASE_SAVE_IS_ON_WHATSAPP_DAYS=7
|
||||||
|
|
||||||
# RabbitMQ - Environment variables
|
# RabbitMQ - Environment variables
|
||||||
RABBITMQ_ENABLED=false
|
RABBITMQ_ENABLED=false
|
||||||
@ -172,12 +173,17 @@ DIFY_ENABLED=false
|
|||||||
# Redis Cache enabled
|
# Redis Cache enabled
|
||||||
CACHE_REDIS_ENABLED=true
|
CACHE_REDIS_ENABLED=true
|
||||||
CACHE_REDIS_URI=redis://localhost:6379/6
|
CACHE_REDIS_URI=redis://localhost:6379/6
|
||||||
|
CACHE_REDIS_TTL=604800
|
||||||
# Prefix serves to differentiate data from one installation to another that are using the same redis
|
# Prefix serves to differentiate data from one installation to another that are using the same redis
|
||||||
CACHE_REDIS_PREFIX_KEY=evolution
|
CACHE_REDIS_PREFIX_KEY=evolution
|
||||||
# Enabling this variable will save the connection information in Redis and not in the database.
|
# Enabling this variable will save the connection information in Redis and not in the database.
|
||||||
CACHE_REDIS_SAVE_INSTANCES=false
|
CACHE_REDIS_SAVE_INSTANCES=false
|
||||||
# Local Cache enabled
|
# Local Cache enabled
|
||||||
CACHE_LOCAL_ENABLED=false
|
CACHE_LOCAL_ENABLED=false
|
||||||
|
# Local Cache enabled
|
||||||
|
CACHE_SAVE_IS_ON_WHATSAPP=true
|
||||||
|
CACHE_REDIS_SAVE_IS_ON_WHATSAPP=false
|
||||||
|
CACHE_REDIS_SAVE_IS_ON_WHATSAPP_DAYS=7
|
||||||
|
|
||||||
# Amazon S3 - Environment variables
|
# Amazon S3 - Environment variables
|
||||||
S3_ENABLED=false
|
S3_ENABLED=false
|
||||||
|
@ -35,11 +35,11 @@ export class CacheService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async set(key: string, value: any) {
|
async set(key: string, value: any, ttl?: number) {
|
||||||
if (!this.cache) {
|
if (!this.cache) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.cache.set(key, value);
|
this.cache.set(key, value, ttl);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async hSet(key: string, field: string, value: any) {
|
public async hSet(key: string, field: string, value: any) {
|
||||||
|
@ -44,6 +44,7 @@ export type SaveData = {
|
|||||||
CHATS: boolean;
|
CHATS: boolean;
|
||||||
LABELS: boolean;
|
LABELS: boolean;
|
||||||
IS_ON_WHATSAPP: boolean;
|
IS_ON_WHATSAPP: boolean;
|
||||||
|
IS_ON_WHATSAPP_DAYS: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type DBConnection = {
|
export type DBConnection = {
|
||||||
@ -168,6 +169,8 @@ export type CacheConfRedis = {
|
|||||||
PREFIX_KEY: string;
|
PREFIX_KEY: string;
|
||||||
TTL: number;
|
TTL: number;
|
||||||
SAVE_INSTANCES: boolean;
|
SAVE_INSTANCES: boolean;
|
||||||
|
SAVE_IS_ON_WHATSAPP: boolean;
|
||||||
|
SAVE_IS_ON_WHATSAPP_TTL: number;
|
||||||
};
|
};
|
||||||
export type CacheConfLocal = {
|
export type CacheConfLocal = {
|
||||||
ENABLED: boolean;
|
ENABLED: boolean;
|
||||||
@ -297,6 +300,7 @@ export class ConfigService {
|
|||||||
HISTORIC: process.env?.DATABASE_SAVE_DATA_HISTORIC === 'true',
|
HISTORIC: process.env?.DATABASE_SAVE_DATA_HISTORIC === 'true',
|
||||||
LABELS: process.env?.DATABASE_SAVE_DATA_LABELS === 'true',
|
LABELS: process.env?.DATABASE_SAVE_DATA_LABELS === 'true',
|
||||||
IS_ON_WHATSAPP: process.env?.DATABASE_SAVE_IS_ON_WHATSAPP === 'true',
|
IS_ON_WHATSAPP: process.env?.DATABASE_SAVE_IS_ON_WHATSAPP === 'true',
|
||||||
|
IS_ON_WHATSAPP_DAYS: Number.parseInt(process.env?.DATABASE_SAVE_IS_ON_WHATSAPP_DAYS ?? '7'),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
RABBITMQ: {
|
RABBITMQ: {
|
||||||
@ -453,6 +457,9 @@ export class ConfigService {
|
|||||||
PREFIX_KEY: process.env?.CACHE_REDIS_PREFIX_KEY || 'evolution-cache',
|
PREFIX_KEY: process.env?.CACHE_REDIS_PREFIX_KEY || 'evolution-cache',
|
||||||
TTL: Number.parseInt(process.env?.CACHE_REDIS_TTL) || 604800,
|
TTL: Number.parseInt(process.env?.CACHE_REDIS_TTL) || 604800,
|
||||||
SAVE_INSTANCES: process.env?.CACHE_REDIS_SAVE_INSTANCES === 'true',
|
SAVE_INSTANCES: process.env?.CACHE_REDIS_SAVE_INSTANCES === 'true',
|
||||||
|
SAVE_IS_ON_WHATSAPP: process.env?.CACHE_REDIS_SAVE_IS_ON_WHATSAPP === 'true',
|
||||||
|
SAVE_IS_ON_WHATSAPP_TTL:
|
||||||
|
(Number.parseInt(process.env?.CACHE_REDIS_SAVE_IS_ON_WHATSAPP_TTL) || 7) * 24 * 60 * 60,
|
||||||
},
|
},
|
||||||
LOCAL: {
|
LOCAL: {
|
||||||
ENABLED: process.env?.CACHE_LOCAL_ENABLED === 'true',
|
ENABLED: process.env?.CACHE_LOCAL_ENABLED === 'true',
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
import { prismaRepository } from '@api/server.module';
|
import { cache, prismaRepository } from '@api/server.module';
|
||||||
import { configService, Database } from '@config/env.config';
|
import { CacheConf, configService, Database } from '@config/env.config';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
|
|
||||||
const ON_WHATSAPP_CACHE_EXPIRATION = 7; // days
|
|
||||||
|
|
||||||
function getAvailableNumbers(remoteJid: string) {
|
function getAvailableNumbers(remoteJid: string) {
|
||||||
const numbersAvailable: string[] = [];
|
const numbersAvailable: string[] = [];
|
||||||
|
|
||||||
@ -56,6 +54,23 @@ interface ISaveOnWhatsappCacheParams {
|
|||||||
remoteJid: string;
|
remoteJid: string;
|
||||||
}
|
}
|
||||||
export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
|
export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
|
||||||
|
const cacheConfig = configService.get<CacheConf>('CACHE');
|
||||||
|
|
||||||
|
if (cacheConfig.REDIS.ENABLED && cacheConfig.REDIS.SAVE_IS_ON_WHATSAPP) {
|
||||||
|
await Promise.all(
|
||||||
|
data.map(async (item) => {
|
||||||
|
const remoteJid = item.remoteJid.startsWith('+') ? item.remoteJid.slice(1) : item.remoteJid;
|
||||||
|
const numbersAvailable = getAvailableNumbers(remoteJid);
|
||||||
|
|
||||||
|
await cache.set(
|
||||||
|
`isOnWhatsapp:${remoteJid}`,
|
||||||
|
JSON.stringify({ jidOptions: numbersAvailable }),
|
||||||
|
cacheConfig.REDIS.SAVE_IS_ON_WHATSAPP_TTL,
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP) {
|
if (configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP) {
|
||||||
const upsertsQuery = data.map((item) => {
|
const upsertsQuery = data.map((item) => {
|
||||||
const remoteJid = item.remoteJid.startsWith('+') ? item.remoteJid.slice(1) : item.remoteJid;
|
const remoteJid = item.remoteJid.startsWith('+') ? item.remoteJid.slice(1) : item.remoteJid;
|
||||||
@ -73,6 +88,43 @@ export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getOnWhatsappCache(remoteJids: string[]) {
|
export async function getOnWhatsappCache(remoteJids: string[]) {
|
||||||
|
const cacheConfig = configService.get<CacheConf>('CACHE');
|
||||||
|
|
||||||
|
const results: {
|
||||||
|
remoteJid: string;
|
||||||
|
number: string;
|
||||||
|
jidOptions: string[];
|
||||||
|
}[] = [];
|
||||||
|
|
||||||
|
if (cacheConfig.REDIS.ENABLED && cacheConfig.REDIS.SAVE_IS_ON_WHATSAPP) {
|
||||||
|
const data = await Promise.all(
|
||||||
|
remoteJids.map(async (remoteJid) => {
|
||||||
|
const remoteJidWithoutPlus = remoteJid.startsWith('+') ? remoteJid.slice(1) : remoteJid;
|
||||||
|
const cacheData = await cache.get(`isOnWhatsapp:${remoteJidWithoutPlus}`);
|
||||||
|
|
||||||
|
if (cacheData) {
|
||||||
|
return {
|
||||||
|
remoteJid: remoteJidWithoutPlus,
|
||||||
|
number: remoteJidWithoutPlus.split('@')[0],
|
||||||
|
jidOptions: JSON.parse(cacheData)?.jidOptions,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
data.forEach((item) => {
|
||||||
|
if (item) {
|
||||||
|
results.push({
|
||||||
|
remoteJid: item.remoteJid,
|
||||||
|
number: item.number,
|
||||||
|
jidOptions: item.jidOptions,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP) {
|
if (configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP) {
|
||||||
const remoteJidsWithoutPlus = remoteJids.map((remoteJid) => getAvailableNumbers(remoteJid)).flat();
|
const remoteJidsWithoutPlus = remoteJids.map((remoteJid) => getAvailableNumbers(remoteJid)).flat();
|
||||||
|
|
||||||
@ -80,17 +132,19 @@ export async function getOnWhatsappCache(remoteJids: string[]) {
|
|||||||
where: {
|
where: {
|
||||||
OR: remoteJidsWithoutPlus.map((remoteJid) => ({ jidOptions: { contains: remoteJid } })),
|
OR: remoteJidsWithoutPlus.map((remoteJid) => ({ jidOptions: { contains: remoteJid } })),
|
||||||
updatedAt: {
|
updatedAt: {
|
||||||
gte: dayjs().subtract(ON_WHATSAPP_CACHE_EXPIRATION, 'days').toDate(),
|
gte: dayjs().subtract(configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP_DAYS, 'days').toDate(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return onWhatsappCache.map((item) => ({
|
onWhatsappCache.forEach((item) =>
|
||||||
remoteJid: item.remoteJid,
|
results.push({
|
||||||
number: item.remoteJid.split('@')[0],
|
remoteJid: item.remoteJid,
|
||||||
jidOptions: item.jidOptions.split(','),
|
number: item.remoteJid.split('@')[0],
|
||||||
}));
|
jidOptions: item.jidOptions.split(','),
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
return results;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user