mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] adaptive availability cache ttl
This commit is contained in:
@@ -8,6 +8,8 @@ const RESOLVED_URL_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|resolved`;
|
|||||||
|
|
||||||
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 AVAILABILITY_TTL = 8 * 60 * 60; // 8 hours
|
||||||
|
const AVAILABILITY_EMPTY_TTL = 60 * 60; // 1 hours
|
||||||
const RESOLVED_URL_TTL = 60; // 1 minutes
|
const RESOLVED_URL_TTL = 60; // 1 minutes
|
||||||
// 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
|
||||||
|
|
||||||
@@ -67,14 +69,13 @@ function cacheWrapResolvedUrl(id, method) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function cacheAvailabilityResults(results) {
|
function cacheAvailabilityResults(results) {
|
||||||
const flatResults = Object.keys(results)
|
Object.keys(results)
|
||||||
.map(infoHash => [`${AVAILABILITY_KEY_PREFIX}:${infoHash}`, results[infoHash]])
|
.forEach(infoHash => {
|
||||||
.reduce((a, b) => a.concat(b), []);
|
const key = `${AVAILABILITY_KEY_PREFIX}:${infoHash}`;
|
||||||
memoryCache.mset(...flatResults, { ttl: STREAM_TTL }, (error) => {
|
const value = results[infoHash];
|
||||||
if (error) {
|
const ttl = value && value.length ? AVAILABILITY_TTL : AVAILABILITY_EMPTY_TTL;
|
||||||
console.log('Failed storing availability cache', error);
|
memoryCache.set(key, value, ttl)
|
||||||
}
|
});
|
||||||
});
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user