From f400a023a3d742efd6e33a45e6b7a72d27d1ffed Mon Sep 17 00:00:00 2001 From: adriano Date: Fri, 11 Nov 2022 19:44:27 -0300 Subject: [PATCH] =?UTF-8?q?Altera=C3=A7=C3=A3o=20para=20atualizar=20o=20ca?= =?UTF-8?q?che=20apos=20o=20bulk=20insert=20contacts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/controllers/ContactController.ts | 16 +++++++++---- backend/src/helpers/ContactsCache.ts | 25 +++++++++++++++++++- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/backend/src/controllers/ContactController.ts b/backend/src/controllers/ContactController.ts index 0cdda94..5a125ae 100644 --- a/backend/src/controllers/ContactController.ts +++ b/backend/src/controllers/ContactController.ts @@ -14,7 +14,8 @@ import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl"; import AppError from "../errors/AppError"; -import { searchContactCache } from '../helpers/ContactsCache' +import { searchContactCache, insertContactsCache } from '../helpers/ContactsCache' + import { off } from "process"; @@ -49,7 +50,7 @@ export const index = async (req: Request, res: Response): Promise => { const data = await searchContactCache(searchParam, offset, 20) - if (data) { + if (data) { console.log('QUERY CONTACTS FROM CACHE QUERY LENGTH: ', data.length) @@ -178,7 +179,7 @@ export const contacsBulkInsertOnQueue = async (req: Request, res: Response): Pro // console.log('THE BODY: ', req.body) - const { adminId, identifier, queueStatus, file } = req.body + const { adminId, identifier, queueStatus, file, contacts_inserted } = req.body const io = getIO(); io.emit("contactsBulkInsertOnQueueStatus", { @@ -190,7 +191,14 @@ export const contacsBulkInsertOnQueue = async (req: Request, res: Response): Pro file: file } }); + - return res.status(200).json({ message: 'ok' }) + if (process.env.CACHE && contacts_inserted) { + + await insertContactsCache(contacts_inserted) + + } + + return res.status(200).json({ message: 'ok' }) }; diff --git a/backend/src/helpers/ContactsCache.ts b/backend/src/helpers/ContactsCache.ts index 20eaf89..1cb7835 100644 --- a/backend/src/helpers/ContactsCache.ts +++ b/backend/src/helpers/ContactsCache.ts @@ -162,6 +162,28 @@ const escapeCharCache = (str: string) => { } +const insertContactsCache = async (contacts: any) => { + + const redis: any = await redisConn(); + + if(!redis) return + + if (redis.status !== 'connect') return + + const pipeline = redis.pipeline() + + for (let i = 0; i < contacts.length; i++) { + + contacts[i].escaped_name = escapeCharCache(contacts[i].name) + + pipeline.hmset(`contact:${contacts[i].id}`, contacts[i]); + } + + await pipeline.exec(() => { console.log(`${contacts.length} CONTACTS INSERTED IN CACHE!`) }); + + redis.quit() +} + const loadContactsCache = async () => { await createContactIndexCache('idx_contact') @@ -225,5 +247,6 @@ export { deleteContactsByIdCache, updateContactCacheById, createOrUpdateContactCache, - escapeCharCache + escapeCharCache, + insertContactsCache } \ No newline at end of file