extend RD cache entries on each access and remove entry when it's not instant
Some checks failed
Deploy Addon / build (SSH_HOST_2, SSH_KEY_2) (push) Has been cancelled
Deploy Addon / build (SSH_HOST_3, SSH_KEY_3) (push) Has been cancelled
Deploy Addon / build (SSH_HOST_4, SSH_KEY_4) (push) Has been cancelled

This commit is contained in:
TheBeastLT 2024-11-23 17:00:54 +02:00
parent 6b4d91f6e0
commit 061f6f45e0
2 changed files with 20 additions and 5 deletions

View file

@ -10,7 +10,7 @@ const RESOLVED_URL_KEY_PREFIX = `${GLOBAL_KEY_PREFIX}|resolved`;
const STREAM_TTL = 24 * 60 * 60 * 1000; // 24 hours
const STREAM_EMPTY_TTL = 60 * 1000; // 1 minute
const RESOLVED_URL_TTL = 3 * 60 * 60 * 1000; // 3 hours
const AVAILABILITY_TTL = 8 * 60 * 60 * 1000; // 8 hours
const AVAILABILITY_TTL = 24 * 60 * 60 * 1000; // 24 hours
const MESSAGE_VIDEO_URL_TTL = 60 * 1000; // 1 minutes
// When the streams are empty we want to cache it for less time in case of timeouts or failures
@ -57,10 +57,23 @@ export function cacheAvailabilityResults(infoHash, fileIds) {
const newResult = result || [];
if (!containsFileIds(newResult)) {
newResult.push(fileIds);
return remoteCache.set(key, newResult, AVAILABILITY_TTL);
newResult.sort((a, b) => b.length - a.length);
}
return newResult
})
return remoteCache.set(key, newResult, AVAILABILITY_TTL);
});
}
export function removeAvailabilityResults(infoHash, fileIds) {
const key = `${AVAILABILITY_KEY_PREFIX}:${infoHash}`;
const fileIdsString = fileIds.toString();
return remoteCache.get(key)
.then(result => {
const storedIndex = result?.findIndex(ids => ids.toString() === fileIdsString);
if (storedIndex >= 0) {
result.splice(storedIndex, 1);
return remoteCache.set(key, result, AVAILABILITY_TTL);
}
});
}
export function getCachedAvailabilityResults(infoHashes) {

View file

@ -2,7 +2,7 @@ import RealDebridClient from 'real-debrid-api';
import { Type } from '../lib/types.js';
import { isVideo, isArchive } from '../lib/extension.js';
import { delay } from '../lib/promises.js';
import { cacheAvailabilityResults, getCachedAvailabilityResults } from '../lib/cache.js';
import { cacheAvailabilityResults, getCachedAvailabilityResults, removeAvailabilityResults } from '../lib/cache.js';
import StaticResponse from './static.js';
import { getMagnetLink } from '../lib/magnetHelper.js';
import { BadTokenError, AccessDeniedError } from './mochHelper.js';
@ -148,6 +148,8 @@ async function _resolve(RD, infoHash, fileIndex, isBrowser) {
return _unrestrictLink(RD, torrent, fileIndex, isBrowser);
} else if (torrent && statusDownloading(torrent.status)) {
console.log(`Downloading to RealDebrid ${infoHash} [${fileIndex}]...`);
const cachedFileIds = torrent.files.filter(file => file.selected).map(file => file.id);
removeAvailabilityResults(infoHash, cachedFileIds);
return StaticResponse.DOWNLOADING;
} else if (torrent && statusMagnetError(torrent.status)) {
console.log(`Failed RealDebrid opening torrent ${infoHash} [${fileIndex}] due to magnet error`);