mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] adds experimental realdebrid support for cached torrents
This commit is contained in:
@@ -3,6 +3,7 @@ const mangodbStore = require('cache-manager-mongodb');
|
||||
|
||||
const GLOBAL_KEY_PREFIX = 'torrentio-addon';
|
||||
const STREAM_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|stream`;
|
||||
const REALDEBRID_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|realdebrid`;
|
||||
|
||||
const STREAM_TTL = process.env.STREAM_TTL || 4 * 60 * 60; // 4 hours
|
||||
const STREAM_EMPTY_TTL = process.env.STREAM_EMPTY_TTL || 30 * 60; // 30 minutes
|
||||
@@ -11,7 +12,8 @@ const STREAM_EMPTY_TTL = process.env.STREAM_EMPTY_TTL || 30 * 60; // 30 minutes
|
||||
const MONGO_URI = process.env.MONGODB_URI;
|
||||
const NO_CACHE = process.env.NO_CACHE || false;
|
||||
|
||||
const cache = initiateCache();
|
||||
const remoteCache = initiateCache();
|
||||
const memoryCache = initiateMemoryCache();
|
||||
|
||||
function initiateCache() {
|
||||
if (NO_CACHE) {
|
||||
@@ -35,7 +37,14 @@ function initiateCache() {
|
||||
}
|
||||
}
|
||||
|
||||
function cacheWrap(key, method, options) {
|
||||
function initiateMemoryCache() {
|
||||
return cacheManager.caching({
|
||||
store: 'memory',
|
||||
ttl: 60
|
||||
});
|
||||
}
|
||||
|
||||
function cacheWrap(cache, key, method, options) {
|
||||
if (NO_CACHE || !cache) {
|
||||
return method();
|
||||
}
|
||||
@@ -43,10 +52,14 @@ function cacheWrap(key, method, options) {
|
||||
}
|
||||
|
||||
function cacheWrapStream(id, method) {
|
||||
return cacheWrap(`${STREAM_KEY_PREFIX}:${id}`, method, {
|
||||
return cacheWrap(remoteCache, `${STREAM_KEY_PREFIX}:${id}`, method, {
|
||||
ttl: (streams) => streams.length ? STREAM_TTL : STREAM_EMPTY_TTL
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { cacheWrapStream };
|
||||
function cacheWrapUnrestricted(id, method) {
|
||||
return cacheWrap(memoryCache, `${REALDEBRID_KEY_PREFIX}:${id}`, method, { ttl: 60 });
|
||||
}
|
||||
|
||||
module.exports = { cacheWrapStream, cacheWrapUnrestricted };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user