mirror of
https://github.com/Viren070/tmdb-addon.git
synced 2025-12-01 23:18:11 +01:00
54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
const cacheManager = require('cache-manager');
|
|
const mangodbStore = require('cache-manager-mongodb');
|
|
|
|
const GLOBAL_KEY_PREFIX = 'tmdb-addon';
|
|
const META_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|meta`;
|
|
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 cache = initiateCache();
|
|
|
|
function initiateCache() {
|
|
if (NO_CACHE) {
|
|
return null;
|
|
} else if (!NO_CACHE && MONGO_URI) {
|
|
return cacheManager.caching({
|
|
store: mangodbStore,
|
|
uri: MONGO_URI,
|
|
options: {
|
|
collection: 'tmdb_collection',
|
|
ttl: META_TTL
|
|
},
|
|
ttl: META_TTL,
|
|
ignoreCacheErrors: true
|
|
});
|
|
} else {
|
|
return cacheManager.caching({
|
|
store: 'memory',
|
|
ttl: META_TTL
|
|
});
|
|
}
|
|
}
|
|
|
|
function cacheWrap(key, method, options) {
|
|
if (NO_CACHE || !cache) {
|
|
return method();
|
|
}
|
|
return cache.wrap(key, method, options);
|
|
}
|
|
|
|
function cacheWrapCatalog(id, method) {
|
|
return cacheWrap(`${CATALOG_KEY_PREFIX}:${id}`, method, { ttl: CATALOG_TTL });
|
|
}
|
|
|
|
function cacheWrapMeta(id, method) {
|
|
return cacheWrap(`${META_KEY_PREFIX}:${id}`, method, { ttl: META_TTL });
|
|
}
|
|
|
|
module.exports = { cacheWrapCatalog, cacheWrapMeta };
|