Merge branch 'redis' into dialogflow

pull/20/head
adriano 2022-11-08 20:32:51 -03:00
commit 6a5fb13560
5 changed files with 63 additions and 4 deletions

View File

@ -47,7 +47,7 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
let { searchParam, pageNumber } = req.query as IndexQuery;
// TEST DEL
if (searchParam && searchParam.trim().length > 0) {
if (searchParam && searchParam.trim().length > 0 && process.env.CACHE) {
try {

View File

@ -10,6 +10,9 @@ import { redisConn } from './TicketCache'
const deleteContactsByIdCache = async (id: string | number) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
const contact_cache: any = await redis.hgetall(`contact:${id}`)
@ -34,6 +37,9 @@ const deleteContactsByIdCache = async (id: string | number) => {
const updateContactCache = async (hash: any, json_object: any) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -53,6 +59,9 @@ const updateContactCache = async (hash: any, json_object: any) => {
const updateContactCacheById = async (id: string | number, update_fields: object | any) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
const contact_cache: any = await redis.hgetall(`contact:${id}`)
@ -81,6 +90,9 @@ const updateContactCacheById = async (id: string | number, update_fields: object
const createOrUpdateContactCache = async (hash: any, contact: any) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
if (contact.name) {
@ -97,6 +109,9 @@ const createOrUpdateContactCache = async (hash: any, contact: any) => {
async function searchContactCache(search: string, offset: number, limit: number) {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return null
search = escapeCharCache(search)
@ -152,6 +167,9 @@ const loadContactsCache = async () => {
await createContactIndexCache('idx_contact')
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
let contacts = await ListContactsServiceCache()
@ -177,6 +195,8 @@ const createContactIndexCache = async (hashIndex: string) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
try {

View File

@ -10,6 +10,10 @@ import { escapeCharCache } from './ContactsCache'
const redisConn = async () => {
if(!process.env.CACHE){
return null
}
try {
const redis = new Redis();
@ -51,6 +55,8 @@ const flushCache = async () => {
const redis: any = await redisConn();
if(!redis) return false
if (redis.status === 'connect') {
console.log('TICKETS CACHE REMOVED')
@ -64,6 +70,8 @@ const cacheSize = async () => {
const redis: any = await redisConn();
if(!redis) return null
if (redis.status !== 'connect') {
return -1
}
@ -78,6 +86,8 @@ const loadTicketsCache = async () => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
await createTicketIndexCache('idx_ticket')
@ -112,6 +122,8 @@ const createTicketIndexCache = async (hashIndex: string) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
try {
@ -138,6 +150,8 @@ const updateTicketCache = async (hash: any, json_object: any) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -156,6 +170,9 @@ const updateTicketCache = async (hash: any, json_object: any) => {
const updateTicketCacheByTicketId = async (ticketId: string | number, update_fields: any) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
const ticket_cache = await redis.hgetall(`ticket:${ticketId}`)
@ -187,6 +204,9 @@ const updateTicketCacheByTicketId = async (ticketId: string | number, update_fie
const createOrUpdateTicketCache = async (hash: any, ticket: any) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
if (redis.status !== 'connect') return
@ -207,6 +227,9 @@ const createOrUpdateTicketCache = async (hash: any, ticket: any) => {
const deleteTicketsByIdCache = async (ticketId: string | number) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
const ticket_cache = await redis.hgetall(`ticket:${ticketId}`)
@ -232,6 +255,9 @@ const deleteTicketsByIdCache = async (ticketId: string | number) => {
const deleteTicketsFieldsCache = async (tickets: any, del_fields: any) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -261,6 +287,9 @@ const deleteTicketsFieldsCache = async (tickets: any, del_fields: any) => {
const updateTicketsByContactsCache = async (oldNumber: string, newName: string, newNumber: string) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -295,6 +324,9 @@ const updateTicketsByContactsCache = async (oldNumber: string, newName: string,
const deleteTicketsByContactsCache = async (number: string) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
@ -324,6 +356,9 @@ const deleteTicketsByContactsCache = async (number: string) => {
const deleteTicketCache = async (hash: any) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
await redis.del(hash)
@ -335,6 +370,8 @@ async function searchTicketCache(search: string, offset?: number, limit?: number
const redis:any = await redisConn();
if(!redis) return
if(redis.status!=='connect') return null
search = escapeCharCache(search)

View File

@ -22,6 +22,8 @@ gracefulShutdown(server);
const cacheLength = await cacheSize()
console.log('cacheSize: ', cacheLength)
if(cacheLength == 0){
console.log('Loading from cache...')
await flushCache()

View File

@ -53,7 +53,7 @@ const ListTicketsService = async ({
let whereCondition: Filterable["where"] = { [Op.or]: [{ userId }, { status: "pending" }], queueId: { [Op.or]: [queueIds, null] } };
if (searchParam && searchParam.trim().length > 0) {
if (searchParam && searchParam.trim().length > 0 && process.env.CACHE) {
try {