[addon] adaptive availability cache ttl

This commit is contained in:
TheBeastLT
2021-06-18 21:34:26 +02:00
parent 20472015c1
commit 2c85ca2ec4

View File

@@ -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_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
// 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) {
const flatResults = Object.keys(results)
.map(infoHash => [`${AVAILABILITY_KEY_PREFIX}:${infoHash}`, results[infoHash]])
.reduce((a, b) => a.concat(b), []);
memoryCache.mset(...flatResults, { ttl: STREAM_TTL }, (error) => {
if (error) {
console.log('Failed storing availability cache', error);
}
});
Object.keys(results)
.forEach(infoHash => {
const key = `${AVAILABILITY_KEY_PREFIX}:${infoHash}`;
const value = results[infoHash];
const ttl = value && value.length ? AVAILABILITY_TTL : AVAILABILITY_EMPTY_TTL;
memoryCache.set(key, value, ttl)
});
return results;
}