Fix: Redis call chain #147
							
								
								
									
										18
									
								
								src/cache/redis.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										18
									
								
								src/cache/redis.js
									
									
									
									
										vendored
									
									
								
							| @@ -1,9 +1,9 @@ | |||||||
| import redis from "redis"; | import redis from "redis"; | ||||||
| import Configuration from "../config/configuration.js"; | import Configuration from "../config/configuration.js"; | ||||||
|  | import redisMockClient from "./redisMock.js"; | ||||||
|  |  | ||||||
| const configuration = Configuration.getInstance(); | const configuration = Configuration.getInstance(); | ||||||
| let client; | let client; | ||||||
| const mockCache = {}; |  | ||||||
|  |  | ||||||
| try { | try { | ||||||
|   console.log("Trying to connect with redis.."); // eslint-disable-line no-console |   console.log("Trying to connect with redis.."); // eslint-disable-line no-console | ||||||
| @@ -20,21 +20,7 @@ try { | |||||||
|     client.quit(); |     client.quit(); | ||||||
|     console.error("Unable to connect to redis, setting up redis-mock."); // eslint-disable-line no-console |     console.error("Unable to connect to redis, setting up redis-mock."); // eslint-disable-line no-console | ||||||
|  |  | ||||||
|     client = { |     client = redisMockClient; | ||||||
|       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 |  | ||||||
|       } |  | ||||||
|     }; |  | ||||||
|   }); |   }); | ||||||
| } catch (e) {} | } 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; | ||||||
| @@ -116,7 +116,7 @@ class Plex { | |||||||
|       headers: { Accept: "application/json" } |       headers: { Accept: "application/json" } | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     return new Promise((resolve, reject) => |     return new Promise((resolve, reject) => { | ||||||
|       this.cache |       this.cache | ||||||
|         .get(cacheKey) |         .get(cacheKey) | ||||||
|         .then(machineInfo => resolve(machineInfo?.machineIdentifier)) |         .then(machineInfo => resolve(machineInfo?.machineIdentifier)) | ||||||
| @@ -132,8 +132,8 @@ class Plex { | |||||||
|           } |           } | ||||||
|  |  | ||||||
|           reject(new PlexUnexpectedError()); |           reject(new PlexUnexpectedError()); | ||||||
|         }) |         }); | ||||||
|     ); |     }); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   async existsInPlex(tmdb) { |   async existsInPlex(tmdb) { | ||||||
| @@ -185,18 +185,24 @@ class Plex { | |||||||
|  |  | ||||||
|     const url = `http://${this.plexIP}:${ |     const url = `http://${this.plexIP}:${ | ||||||
|       this.plexPort |       this.plexPort | ||||||
|     }/hubs/search?query=${fixedEncodeURIComponent(query)}`; |     }/hubs/search?query=${fixedEncodeURIComponent(query)}&X-Plex-Token=${ | ||||||
|  |       this.token | ||||||
|  |     }`; | ||||||
|  |  | ||||||
|     const options = { |     const options = { | ||||||
|       timeout: 20000, |       timeout: 20000, | ||||||
|       headers: { Accept: "application/json" } |       headers: { Accept: "application/json" } | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     return new Promise((resolve, reject) => |     return new Promise((resolve, reject) => { | ||||||
|       this.cache |       this.cache | ||||||
|         .get(cacheKey) |         .get(cacheKey) | ||||||
|         .catch(() => fetch(url, options)) // else fetch fresh data |         .catch(() => { | ||||||
|         .then(successfullResponse) |           // else fetch fresh data | ||||||
|         .then(results => this.cache.set(cacheKey, results, 21600)) // 6 hours |           return fetch(url, options) | ||||||
|  |             .then(successfullResponse) | ||||||
|  |             .then(results => this.cache.set(cacheKey, results, 21600)); // 6 hours | ||||||
|  |         }) | ||||||
|         .then(mapResults) |         .then(mapResults) | ||||||
|         .then(resolve) |         .then(resolve) | ||||||
|         .catch(error => { |         .catch(error => { | ||||||
| @@ -205,8 +211,8 @@ class Plex { | |||||||
|           } |           } | ||||||
|  |  | ||||||
|           reject(new PlexUnexpectedError()); |           reject(new PlexUnexpectedError()); | ||||||
|         }) |         }); | ||||||
|     ); |     }); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   // this is not guarenteed to work, but if we see a movie or |   // this is not guarenteed to work, but if we see a movie or | ||||||
| @@ -221,7 +227,7 @@ class Plex { | |||||||
|       // TODO improve cache key matching by lowercasing it on the backend. |       // 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 |       // what do we actually need to check for if the key was deleted or not | ||||||
|       // it might be an error or another response code. |       // 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; |       return response === 1; | ||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user