refactor(getCache): switch from MongoDB to Redis for caching implementation

This commit is contained in:
mrcanelas
2025-07-02 14:17:07 -03:00
parent de8de39be4
commit 5a4a652e67
+9 -12
View File
@@ -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 };
module.exports = { cacheWrapCatalog, cacheWrapMeta, cache };