53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
const cacheManager = require('cache-manager');
|
|
const mangodbStore = require('cache-manager-mongodb');
|
|
|
|
const GLOBAL_KEY_PREFIX = 'torrentio-addon';
|
|
const STREAM_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|stream`;
|
|
|
|
const STREAM_TTL = process.env.STREAM_TTL || 24 * 60 * 60; // 24 hours
|
|
const STREAM_EMPTY_TTL = process.env.STREAM_EMPTY_TTL || 30 * 60; // 30 minutes
|
|
// When the streams are empty we want to cache it for less time in case of timeouts or failures
|
|
|
|
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 (MONGO_URI) {
|
|
return cacheManager.caching({
|
|
store: mangodbStore,
|
|
uri: MONGO_URI,
|
|
options: {
|
|
collection: 'torrentio-addon',
|
|
ttl: STREAM_TTL
|
|
},
|
|
ttl: STREAM_TTL,
|
|
ignoreCacheErrors: true
|
|
});
|
|
} else {
|
|
return cacheManager.caching({
|
|
store: 'memory',
|
|
ttl: STREAM_TTL
|
|
});
|
|
}
|
|
}
|
|
|
|
function cacheWrap(key, method, options) {
|
|
if (NO_CACHE || !cache) {
|
|
return method();
|
|
}
|
|
return cache.wrap(key, method, options);
|
|
}
|
|
|
|
function cacheWrapStream(id, method) {
|
|
return cacheWrap(`${STREAM_KEY_PREFIX}:${id}`, method, {
|
|
ttl: (streams) => streams.length ? STREAM_TTL : STREAM_EMPTY_TTL
|
|
});
|
|
}
|
|
|
|
module.exports = { cacheWrapStream };
|
|
|