Skip to content

Commit

Permalink
extend RD cache entries on each access and remove entry when it's not…
Browse files Browse the repository at this point in the history
… instant
  • Loading branch information
TheBeastLT committed Nov 23, 2024
1 parent 6b4d91f commit 061f6f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 17 additions & 4 deletions addon/lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion addon/moch/realdebrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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`);
Expand Down

0 comments on commit 061f6f4

Please sign in to comment.