Correção para não instanciar o redis

pull/21/head
adriano 2023-01-06 07:08:40 -03:00
parent 37ab4b9564
commit 8059781c92
1 changed files with 37 additions and 33 deletions

View File

@ -7,12 +7,16 @@ import ListTicketServiceCache from "../services/TicketServices/ListTicketService
import { escapeCharCache } from './ContactsCache' import { escapeCharCache } from './ContactsCache'
const redis = new Redis(); let redis: any = null
if (process.env.CACHE) {
redis = new Redis();
}
const redisConn = async () => { const redisConn = async () => {
if(!process.env.CACHE){ if (!process.env.CACHE) {
return null return null
} }
@ -68,7 +72,7 @@ const flushCache = async () => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return false if (!redis) return false
// if (redis.status === 'connect') { // if (redis.status === 'connect') {
@ -85,7 +89,7 @@ const cacheSize = async () => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return null if (!redis) return null
// if (redis.status !== 'connect') return -1 // if (redis.status !== 'connect') return -1
@ -99,9 +103,9 @@ const loadTicketsCache = async () => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
// if (redis.status !== 'connect') return // if (redis.status !== 'connect') return
await createTicketIndexCache('idx_ticket') await createTicketIndexCache('idx_ticket')
@ -135,9 +139,9 @@ const createTicketIndexCache = async (hashIndex: string) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
// if (redis.status !== 'connect') return // if (redis.status !== 'connect') return
try { try {
@ -163,9 +167,9 @@ const updateTicketCache = async (hash: any, json_object: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
// if (redis.status !== 'connect') return // if (redis.status !== 'connect') return
const pipeline = redis.pipeline() const pipeline = redis.pipeline()
let entries = Object.entries(json_object) let entries = Object.entries(json_object)
@ -184,9 +188,9 @@ const updateTicketCacheByTicketId = async (ticketId: string | number, update_fie
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
// if (redis.status !== 'connect') return // if (redis.status !== 'connect') return
const ticket_cache = await redis.hgetall(`ticket:${ticketId}`) const ticket_cache = await redis.hgetall(`ticket:${ticketId}`)
@ -218,9 +222,9 @@ const createOrUpdateTicketCache = async (hash: any, ticket: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
// if (redis.status !== 'connect') return // if (redis.status !== 'connect') return
ticket.escaped_name = escapeCharCache(ticket['contact.name']) ticket.escaped_name = escapeCharCache(ticket['contact.name'])
@ -239,9 +243,9 @@ const deleteTicketsByIdCache = async (ticketId: string | number) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
// if (redis.status !== 'connect') return // if (redis.status !== 'connect') return
const ticket_cache = await redis.hgetall(`ticket:${ticketId}`) const ticket_cache = await redis.hgetall(`ticket:${ticketId}`)
@ -267,9 +271,9 @@ const deleteTicketsFieldsCache = async (tickets: any, del_fields: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
// if (redis.status !== 'connect') return // if (redis.status !== 'connect') return
const pipeline = redis.pipeline() const pipeline = redis.pipeline()
@ -299,9 +303,9 @@ const updateTicketsByContactsCache = async (oldNumber: string, newName: string,
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
// if (redis.status !== 'connect') return // if (redis.status !== 'connect') return
const pipeline = redis.pipeline() const pipeline = redis.pipeline()
@ -336,9 +340,9 @@ const deleteTicketsByContactsCache = async (number: string) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
// if (redis.status !== 'connect') return // if (redis.status !== 'connect') return
const pipeline = redis.pipeline() const pipeline = redis.pipeline()
@ -368,9 +372,9 @@ const deleteTicketCache = async (hash: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
// if (redis.status !== 'connect') return // if (redis.status !== 'connect') return
await redis.del(hash) await redis.del(hash)
// await redis.quit() // await redis.quit()
@ -379,9 +383,9 @@ const deleteTicketCache = async (hash: any) => {
async function searchTicketCache(search: string, offset?: number, limit?: number) { async function searchTicketCache(search: string, offset?: number, limit?: number) {
const redis:any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
// if(redis.status!=='connect') return null // if(redis.status!=='connect') return null