Fix: Redis call chain (#147)

* Moved redis mock client to separate file

* To keep cache response & api response consistent, alter call chain

* Resolved linting issues
This commit was merged in pull request #147.
This commit is contained in:
2022-09-25 13:39:48 +02:00
committed by GitHub
parent c3f041d27f
commit 212a6864f1
3 changed files with 38 additions and 27 deletions

18
src/cache/redis.js vendored
View File

@@ -1,9 +1,9 @@
import redis from "redis";
import Configuration from "../config/configuration.js";
import redisMockClient from "./redisMock.js";
const configuration = Configuration.getInstance();
let client;
const mockCache = {};
try {
console.log("Trying to connect with redis.."); // eslint-disable-line no-console
@@ -20,21 +20,7 @@ try {
client.quit();
console.error("Unable to connect to redis, setting up redis-mock."); // eslint-disable-line no-console
client = {
get(key, callback) {
console.log(`redis-dummy get: ${key}`); // eslint-disable-line no-console
const hit = mockCache[key];
return Promise.resolve().then(callback(null, JSON.parse(hit)));
},
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
}
};
client = redisMockClient;
});
} catch (e) {}

19
src/cache/redisMock.js vendored Normal file
View File

@@ -0,0 +1,19 @@
const mockCache = {};
const redisMockClient = {
get(key, callback) {
// console.log(`redis-dummy get: ${key}`); // eslint-disable-line no-console
const hit = mockCache[key] || null;
return Promise.resolve(callback(null, hit));
},
set(key, json, callback) {
// console.log(`redis-dummy set: ${key}`); // eslint-disable-line no-console
mockCache[key] = JSON.stringify(json);
return Promise.resolve(callback(null, "OK"));
},
expire(key, TTL) {
console.log(`redis-dummy expire: ${key} with TTL ${TTL}`); // eslint-disable-line no-console
}
};
export default redisMockClient;