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; let { searchParam, pageNumber } = req.query as IndexQuery;
// TEST DEL // TEST DEL
if (searchParam && searchParam.trim().length > 0) { if (searchParam && searchParam.trim().length > 0 && process.env.CACHE) {
try { try {

View File

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

View File

@ -9,6 +9,10 @@ import { escapeCharCache } from './ContactsCache'
const redisConn = async () => { const redisConn = async () => {
if(!process.env.CACHE){
return null
}
try { try {
@ -49,7 +53,9 @@ const redisConn = async () => {
const flushCache = async () => { const flushCache = async () => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return false
if (redis.status === 'connect') { if (redis.status === 'connect') {
@ -64,6 +70,8 @@ const cacheSize = async () => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return null
if (redis.status !== 'connect') { if (redis.status !== 'connect') {
return -1 return -1
} }
@ -78,6 +86,8 @@ const loadTicketsCache = async () => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
await createTicketIndexCache('idx_ticket') await createTicketIndexCache('idx_ticket')
@ -112,6 +122,8 @@ const createTicketIndexCache = async (hashIndex: string) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
try { try {
@ -138,6 +150,8 @@ const updateTicketCache = async (hash: any, json_object: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
const pipeline = redis.pipeline() 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 updateTicketCacheByTicketId = async (ticketId: string | number, update_fields: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
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}`)
@ -187,6 +204,9 @@ const updateTicketCacheByTicketId = async (ticketId: string | number, update_fie
const createOrUpdateTicketCache = async (hash: any, ticket: any) => { const createOrUpdateTicketCache = async (hash: any, ticket: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') 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 deleteTicketsByIdCache = async (ticketId: string | number) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
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}`)
@ -232,6 +255,9 @@ const deleteTicketsByIdCache = async (ticketId: string | number) => {
const deleteTicketsFieldsCache = async (tickets: any, del_fields: any) => { const deleteTicketsFieldsCache = async (tickets: any, del_fields: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
const pipeline = redis.pipeline() 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 updateTicketsByContactsCache = async (oldNumber: string, newName: string, newNumber: string) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
const pipeline = redis.pipeline() const pipeline = redis.pipeline()
@ -295,6 +324,9 @@ const updateTicketsByContactsCache = async (oldNumber: string, newName: string,
const deleteTicketsByContactsCache = async (number: string) => { const deleteTicketsByContactsCache = async (number: string) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
const pipeline = redis.pipeline() const pipeline = redis.pipeline()
@ -324,6 +356,9 @@ const deleteTicketsByContactsCache = async (number: string) => {
const deleteTicketCache = async (hash: any) => { const deleteTicketCache = async (hash: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
await redis.del(hash) await redis.del(hash)
@ -333,7 +368,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.status!=='connect') return null if(redis.status!=='connect') return null

View File

@ -22,6 +22,8 @@ gracefulShutdown(server);
const cacheLength = await cacheSize() const cacheLength = await cacheSize()
console.log('cacheSize: ', cacheLength)
if(cacheLength == 0){ if(cacheLength == 0){
console.log('Loading from cache...') console.log('Loading from cache...')
await flushCache() 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] } }; 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 { try {