Alteração para que o cache seja atualizado apos o bulk insert de contactos

pull/20/head
adriano 2022-11-11 20:49:43 -03:00
commit 82f0f107f4
2 changed files with 37 additions and 6 deletions

View File

@ -13,7 +13,8 @@ import CheckIsValidContact from "../services/WbotServices/CheckIsValidContact";
import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl"; import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
import AppError from "../errors/AppError"; import AppError from "../errors/AppError";
import { searchContactCache } from '../helpers/ContactsCache' import { searchContactCache, insertContactsCache } from '../helpers/ContactsCache'
import { off } from "process"; import { off } from "process";
import GetContactService from "../services/ContactServices/GetContactService"; import GetContactService from "../services/ContactServices/GetContactService";
@ -233,7 +234,7 @@ export const contacsBulkInsertOnQueue = async (req: Request, res: Response): Pro
// console.log('THE BODY: ', req.body) // 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(); const io = getIO();
io.emit("contactsBulkInsertOnQueueStatus", { io.emit("contactsBulkInsertOnQueueStatus", {
@ -246,6 +247,13 @@ export const contacsBulkInsertOnQueue = async (req: Request, res: Response): Pro
} }
}); });
if (process.env.CACHE && contacts_inserted) {
await insertContactsCache(contacts_inserted)
}
return res.status(200).json({ message: 'ok' }) 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 () => { const loadContactsCache = async () => {
await createContactIndexCache('idx_contact') await createContactIndexCache('idx_contact')
@ -225,5 +247,6 @@ export {
deleteContactsByIdCache, deleteContactsByIdCache,
updateContactCacheById, updateContactCacheById,
createOrUpdateContactCache, createOrUpdateContactCache,
escapeCharCache escapeCharCache,
insertContactsCache
} }