Skip to content

Commit

Permalink
cache (#50)
Browse files Browse the repository at this point in the history
* cache
  • Loading branch information
tsaridas authored Jan 31, 2024
1 parent 18c2a59 commit 88f362b
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const respond = (res, data) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', '*');
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'max-age=7200, stale-while-revalidate=14400, stale-if-error=604800, public');
res.send(data);
};

Expand Down Expand Up @@ -85,7 +84,7 @@ async function getConemataInfo(streamInfo, abortSignals) {

const responseBody = response.data;
if (!responseBody || !responseBody.meta || !responseBody.meta.name) {
throw new Error(`Could not get info from Cinemata: ${url} - ${response.statusCode}`);
throw new Error(`Could not get info from Cinemata: ${url} - ${response.status}`);
}

streamInfo.name = responseBody.meta.name;
Expand Down Expand Up @@ -337,12 +336,21 @@ addon.get('/stream/:type/:id.json', async (req, res) => {
const finalData = processTorrentList(streams);
config.debug && console.log("Sliced & Sorted data ", finalData);
console.log(`A: time: ${elapsedTime} / id: ${streamInfo.imdbId} / results: ${finalData.length} / timeout: ${(elapsedTime >= config.responseTimeout)} / search finished: ${searchFinished} / queue idle: ${asyncQueue.idle()} / pending downloads: ${inProgressCount} / discarded: ${(streams.length - finalData.length)}`);
return respond(res, {
streams: finalData,
"cacheMaxAge": 1440,
"staleRevalidate": 240,
"staleError": 10080
});
if (finalData.length > 0) {
res.setHeader('Cache-Control', 'max-age=7200, stale-while-revalidate=14400, stale-if-error=604800, public');
// Set cache-related headers if "streams" contains data
return respond(res, {
streams: finalData,
"cacheMaxAge": 7200,
"staleRevalidate": 14400,
"staleError": 604800
});
} else {
// If "streams" is empty, do not set cache-related headers
return respond(res, {
streams: finalData
});
}
}
config.debug && console.log(`s: id: ${streamInfo.imdbId} / time pending: ${(config.responseTimeout - elapsedTime)} / search finished: ${searchFinished} / queue idle: ${asyncQueue.idle()} / pending downloads: ${inProgressCount} / processed streams: ${streams.length}`);

Expand Down

0 comments on commit 88f362b

Please sign in to comment.