From 6f92419025dbe2667aaaeac94161e49ac9e73feb Mon Sep 17 00:00:00 2001 From: Ali Mahdavi Date: Fri, 14 Jul 2023 22:33:50 +0300 Subject: [PATCH] add querycache to Backlog for faster search --- ClusterOperator/Backlog.js | 6 ++++++ ClusterOperator/server.js | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ClusterOperator/Backlog.js b/ClusterOperator/Backlog.js index 386f3ae..a74be88 100644 --- a/ClusterOperator/Backlog.js +++ b/ClusterOperator/Backlog.js @@ -1,6 +1,7 @@ /* eslint-disable no-else-return */ /* eslint-disable no-restricted-syntax */ // const timer = require('timers/promises'); +const queryCache = require('memory-cache'); const dbClient = require('./DBClient'); const config = require('./config'); const log = require('../lib/log'); @@ -24,6 +25,8 @@ class BackLog { static executeLogs = true; + static BLqueryCache = queryCache; + /** * [createBacklog] * @param {object} params [description] @@ -113,6 +116,9 @@ class BackLog { [seqForThis, query, timestamp], ); if (this.executeLogs) log.info(`executed ${seqForThis}`); + this.BLqueryCache.put(seqForThis, { + query, seq: seqForThis, timestamp, connId, ip: false, + }, 1000 * 30); this.writeLock = false; let result = null; if (connId === false) { diff --git a/ClusterOperator/server.js b/ClusterOperator/server.js index 0c8ae1b..65442ae 100644 --- a/ClusterOperator/server.js +++ b/ClusterOperator/server.js @@ -258,8 +258,12 @@ async function initServer() { socket.emit('query', record.query, record.seq, record.timestamp, connId); } else { log.warn(`query ${index} not in query cache`, 'red'); - const BLRecord = await BackLog.getLog(index); + let BLRecord = BackLog.BLqueryCache.get(index); log.info(JSON.stringify(BLRecord), 'red'); + if (!BLRecord) { + BLRecord = BackLog.getLog(index); + log.info(`from DB : ${JSON.stringify(BLRecord)}`, 'red'); + } } // if (record) {