mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] use mem cache for resolved links
This commit is contained in:
@@ -4,19 +4,16 @@ const mangodbStore = require('cache-manager-mongodb');
|
|||||||
const GLOBAL_KEY_PREFIX = 'torrentio-addon';
|
const GLOBAL_KEY_PREFIX = 'torrentio-addon';
|
||||||
const STREAM_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|stream`;
|
const STREAM_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|stream`;
|
||||||
const RESOLVED_URL_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|resolved`;
|
const RESOLVED_URL_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|resolved`;
|
||||||
const PROXY_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|proxy`;
|
|
||||||
const USER_AGENT_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|agent`;
|
|
||||||
|
|
||||||
const STREAM_TTL = process.env.STREAM_TTL || 4 * 60 * 60; // 4 hours
|
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
|
const STREAM_EMPTY_TTL = process.env.STREAM_EMPTY_TTL || 30 * 60; // 30 minutes
|
||||||
const RESOLVED_URL_TTL = 2 * 60; // 2 minutes
|
const RESOLVED_URL_TTL = 60; // 2 minutes
|
||||||
const PROXY_TTL = 30 * 60; // 60 minutes
|
|
||||||
const USER_AGENT_TTL = 2 * 24 * 60 * 60; // 2 days
|
|
||||||
// When the streams are empty we want to cache it for less time in case of timeouts or failures
|
// 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 MONGO_URI = process.env.MONGODB_URI;
|
||||||
const NO_CACHE = process.env.NO_CACHE || false;
|
const NO_CACHE = process.env.NO_CACHE || false;
|
||||||
|
|
||||||
|
const memoryCache = initiateMemoryCache();
|
||||||
const remoteCache = initiateRemoteCache();
|
const remoteCache = initiateRemoteCache();
|
||||||
|
|
||||||
function initiateRemoteCache() {
|
function initiateRemoteCache() {
|
||||||
@@ -43,6 +40,13 @@ function initiateRemoteCache() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function initiateMemoryCache() {
|
||||||
|
return cacheManager.caching({
|
||||||
|
store: 'memory',
|
||||||
|
ttl: RESOLVED_URL_TTL
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function cacheWrap(cache, key, method, options) {
|
function cacheWrap(cache, key, method, options) {
|
||||||
if (NO_CACHE || !cache) {
|
if (NO_CACHE || !cache) {
|
||||||
return method();
|
return method();
|
||||||
@@ -57,20 +61,8 @@ function cacheWrapStream(id, method) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function cacheWrapResolvedUrl(id, method) {
|
function cacheWrapResolvedUrl(id, method) {
|
||||||
return cacheWrap(remoteCache, `${RESOLVED_URL_KEY_PREFIX}:${id}`, method, { ttl: RESOLVED_URL_TTL });
|
return cacheWrap(memoryCache, `${RESOLVED_URL_KEY_PREFIX}:${id}`, method, { ttl: RESOLVED_URL_TTL });
|
||||||
}
|
}
|
||||||
|
|
||||||
function cacheWrapProxy(id, method) {
|
module.exports = { cacheWrapStream, cacheWrapResolvedUrl };
|
||||||
return cacheWrap(remoteCache, `${PROXY_KEY_PREFIX}:${id}`, method, { ttl: PROXY_TTL });
|
|
||||||
}
|
|
||||||
|
|
||||||
function cacheUserAgent(id, method) {
|
|
||||||
return cacheWrap(remoteCache, `${USER_AGENT_KEY_PREFIX}:${id}`, method, { ttl: USER_AGENT_TTL });
|
|
||||||
}
|
|
||||||
|
|
||||||
function uncacheProxy(id) {
|
|
||||||
return remoteCache.del(`${PROXY_KEY_PREFIX}:${id}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { cacheWrapStream, cacheWrapResolvedUrl, cacheWrapProxy, cacheUserAgent, uncacheProxy };
|
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ const MOCHS = {
|
|||||||
|
|
||||||
const unrestrictQueue = new namedQueue((task, callback) => task.method()
|
const unrestrictQueue = new namedQueue((task, callback) => task.method()
|
||||||
.then(result => callback(false, result))
|
.then(result => callback(false, result))
|
||||||
.catch((error => callback(error))));
|
.catch((error => callback(error))), 5);
|
||||||
|
|
||||||
async function applyMochs(streams, config) {
|
async function applyMochs(streams, config) {
|
||||||
if (!streams || !streams.length || !Object.keys(MOCHS).find(moch => config[moch])) {
|
if (!streams || !streams.length || !Object.keys(MOCHS).find(moch => config[moch])) {
|
||||||
@@ -92,7 +92,7 @@ async function resolve(parameters) {
|
|||||||
console.warn(error);
|
console.warn(error);
|
||||||
return StaticResponse.FAILED_UNEXPECTED;
|
return StaticResponse.FAILED_UNEXPECTED;
|
||||||
});
|
});
|
||||||
|
console.log(`Starting [${parameters.infoHash}] link resolve with queue size: ${unrestrictQueue.length()}`);
|
||||||
return new Promise(((resolve, reject) => {
|
return new Promise(((resolve, reject) => {
|
||||||
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user