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 is contained in:
18
src/cache/redis.js
vendored
18
src/cache/redis.js
vendored
@@ -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
19
src/cache/redisMock.js
vendored
Normal 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;
|
||||
@@ -117,7 +117,7 @@ class Plex {
|
||||
headers: { Accept: "application/json" }
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) =>
|
||||
return new Promise((resolve, reject) => {
|
||||
this.cache
|
||||
.get(cacheKey)
|
||||
.then(machineInfo => resolve(machineInfo?.machineIdentifier))
|
||||
@@ -133,8 +133,8 @@ class Plex {
|
||||
}
|
||||
|
||||
reject(new PlexUnexpectedError());
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async existsInPlex(tmdb) {
|
||||
@@ -186,18 +186,24 @@ class Plex {
|
||||
|
||||
const url = `http://${this.plexIP}:${
|
||||
this.plexPort
|
||||
}/hubs/search?query=${fixedEncodeURIComponent(query)}`;
|
||||
}/hubs/search?query=${fixedEncodeURIComponent(query)}&X-Plex-Token=${
|
||||
this.token
|
||||
}`;
|
||||
|
||||
const options = {
|
||||
timeout: 20000,
|
||||
headers: { Accept: "application/json" }
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) =>
|
||||
return new Promise((resolve, reject) => {
|
||||
this.cache
|
||||
.get(cacheKey)
|
||||
.catch(() => fetch(url, options)) // else fetch fresh data
|
||||
.then(successfullResponse)
|
||||
.then(results => this.cache.set(cacheKey, results, 21600)) // 6 hours
|
||||
.catch(() => {
|
||||
// else fetch fresh data
|
||||
return fetch(url, options)
|
||||
.then(successfullResponse)
|
||||
.then(results => this.cache.set(cacheKey, results, 21600)); // 6 hours
|
||||
})
|
||||
.then(mapResults)
|
||||
.then(resolve)
|
||||
.catch(error => {
|
||||
@@ -206,8 +212,8 @@ class Plex {
|
||||
}
|
||||
|
||||
reject(new PlexUnexpectedError());
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// this is not guarenteed to work, but if we see a movie or
|
||||
@@ -222,7 +228,7 @@ class Plex {
|
||||
// TODO improve cache key matching by lowercasing it on the backend.
|
||||
// what do we actually need to check for if the key was deleted or not
|
||||
// it might be an error or another response code.
|
||||
console.log("Unable to delete, key might not exists");
|
||||
console.log("Unable to delete, key might not exists"); // eslint-disable-line no-console
|
||||
return response === 1;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user