Alteração para atualizar o cache apos o bulk insert contacts

pull/21/head
adriano 2022-11-11 19:44:27 -03:00
parent e83f46ae08
commit f400a023a3
2 changed files with 36 additions and 5 deletions

View File

@ -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<Response> => {
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' })
};

View File

@ -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
}