From 5a4a652e6712e3c762ca2e0e89663d2755e4a6a8 Mon Sep 17 00:00:00 2001 From: mrcanelas Date: Wed, 2 Jul 2025 14:17:07 -0300 Subject: [PATCH] refactor(getCache): switch from MongoDB to Redis for caching implementation --- addon/lib/getCache.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/addon/lib/getCache.js b/addon/lib/getCache.js index dd169df..f0745be 100644 --- a/addon/lib/getCache.js +++ b/addon/lib/getCache.js @@ -1,5 +1,6 @@ const cacheManager = require('cache-manager'); -const mangodbStore = require('cache-manager-mongodb'); +const redisStore = require('cache-manager-ioredis'); +const Redis = require('ioredis'); const GLOBAL_KEY_PREFIX = 'tmdb-addon'; const META_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|meta`; @@ -8,24 +9,20 @@ const CATALOG_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|catalog`; const META_TTL = process.env.META_TTL || 7 * 24 * 60 * 60; // 7 day const CATALOG_TTL = process.env.CATALOG_TTL || 1 * 24 * 60 * 60; // 1 day -const MONGO_URI = process.env.MONGODB_URI; const NO_CACHE = process.env.NO_CACHE || false; +const REDIS_URL = process.env.REDIS_URL; const cache = initiateCache(); function initiateCache() { if (NO_CACHE) { return null; - } else if (!NO_CACHE && MONGO_URI) { + } else if (REDIS_URL) { + const redisInstance = new Redis(REDIS_URL); return cacheManager.caching({ - store: mangodbStore, - uri: MONGO_URI, - options: { - collection: 'tmdb_collection', - ttl: META_TTL - }, - ttl: META_TTL, - ignoreCacheErrors: true + store: redisStore, + redisInstance: redisInstance, + ttl: META_TTL }); } else { return cacheManager.caching({ @@ -50,4 +47,4 @@ function cacheWrapMeta(id, method) { return cacheWrap(`${META_KEY_PREFIX}:${id}`, method, { ttl: META_TTL }); } -module.exports = { cacheWrapCatalog, cacheWrapMeta }; \ No newline at end of file +module.exports = { cacheWrapCatalog, cacheWrapMeta, cache }; \ No newline at end of file