diff --git a/addon/lib/cache.js b/addon/lib/cache.js index cb29dfe..661700f 100644 --- a/addon/lib/cache.js +++ b/addon/lib/cache.js @@ -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) { diff --git a/addon/moch/realdebrid.js b/addon/moch/realdebrid.js index 8d4dd30..6673ab5 100644 --- a/addon/moch/realdebrid.js +++ b/addon/moch/realdebrid.js @@ -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`);