mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
updating to use tpb cached torrent files
This commit is contained in:
36
lib/cache.js
36
lib/cache.js
@@ -1,19 +1,45 @@
|
||||
|
||||
const cacheManager = require('cache-manager');
|
||||
const mangodbStore = require('cache-manager-mongodb');
|
||||
|
||||
const GLOBAL_KEY_PREFIX = 'stremio-torrentio';
|
||||
const IMDB_ID_PREFIX = `${GLOBAL_KEY_PREFIX}|imdb_id`;
|
||||
const METADATA_PREFIX = `${GLOBAL_KEY_PREFIX}|metadata`;
|
||||
const TORRENT_FILES_KEY_PREFIX = `stremio-tpb|files`;
|
||||
|
||||
const GLOBAL_TTL = process.env.METADATA_TTL || 7 * 24 * 60 * 60; // 7 days
|
||||
|
||||
const MONGO_URI = process.env.MONGODB_URI;
|
||||
|
||||
const cache = initiateCache();
|
||||
|
||||
function initiateCache() {
|
||||
return cacheManager.caching({
|
||||
store: 'memory',
|
||||
ttl: GLOBAL_TTL
|
||||
});
|
||||
if (MONGO_URI) {
|
||||
return cacheManager.caching({
|
||||
store: mangodbStore,
|
||||
uri: MONGO_URI,
|
||||
options: {
|
||||
collection: 'cacheManager',
|
||||
},
|
||||
ttl: GLOBAL_TTL,
|
||||
ignoreCacheErrors: true
|
||||
});
|
||||
} else {
|
||||
return cacheManager.caching({
|
||||
store: 'memory',
|
||||
ttl: GLOBAL_TTL
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function retrieveTorrentFiles(infoHash) {
|
||||
return cache.get(`${TORRENT_FILES_KEY_PREFIX}:${infoHash}`)
|
||||
.then((results) => {
|
||||
if (!results) {
|
||||
throw new Error('No cached files found');
|
||||
}
|
||||
return results;
|
||||
});
|
||||
}
|
||||
|
||||
function cacheWrap(key, method, options) {
|
||||
@@ -28,5 +54,5 @@ function cacheWrapMetadata(id, method) {
|
||||
return cacheWrap(`${METADATA_PREFIX}:${id}`, method, { ttl: GLOBAL_TTL });
|
||||
}
|
||||
|
||||
module.exports = { cacheWrapImdbId, cacheWrapMetadata };
|
||||
module.exports = { cacheWrapImdbId, cacheWrapMetadata, retrieveTorrentFiles };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user