From 16469ee2eb30ebcec32d09f2b4321cde286f0790 Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Sat, 20 Aug 2022 16:58:41 +0200 Subject: [PATCH] Fixed redis expire & mock implmentation --- src/cache/redis.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/cache/redis.js b/src/cache/redis.js index fdee990..dee6e5f 100644 --- a/src/cache/redis.js +++ b/src/cache/redis.js @@ -1,6 +1,7 @@ const configuration = require("../config/configuration").getInstance(); let client; +const mockCache = {}; try { const redis = require("redis"); // eslint-disable-line global-require @@ -8,7 +9,6 @@ try { const host = configuration.get("redis", "host"); const port = configuration.get("redis", "port"); - console.log(`redis://${host}:${port}`); // eslint-disable-line no-console client = redis.createClient({ url: `redis://${host}:${port}` }); @@ -20,13 +20,18 @@ try { console.error("Unable to connect to redis, setting up redis-mock."); // eslint-disable-line no-console client = { - get(command) { - console.log(`redis-dummy get: ${command}`); // eslint-disable-line no-console - return Promise.resolve(); + get(key, callback) { + console.log(`redis-dummy get: ${key}`); // eslint-disable-line no-console + const hit = mockCache[key]; + return Promise.resolve().then(callback(null, hit)); }, - set(command) { - console.log(`redis-dummy set: ${command}`); // eslint-disable-line no-console - return Promise.resolve(); + set(key, json, callback) { + console.log(`redis-dummy set: ${key}`); // eslint-disable-line no-console + mockCache[key] = JSON.stringify(json); + return Promise.resolve().then(callback(null, "OK")); + }, + expire(key, TTL) { + console.log(`redis-dummy expire: ${key} with TTL ${TTL}`); // eslint-disable-line no-console } }; }); @@ -39,7 +44,7 @@ function set(key, value, TTL = 10800) { client.set(key, json, (error, reply) => { if (reply === "OK") { // successfully set value with key, now set TTL for key - client.expire(key, TTL, e => { + client.expire(key, TTL, "NX", e => { if (e) // eslint-disable-next-line no-console console.error(