Skip to content

Commit

Permalink
update cache manager usage
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBeastLT committed Nov 16, 2024
1 parent 0a45dde commit 0cd43f1
Show file tree
Hide file tree
Showing 4 changed files with 802 additions and 715 deletions.
2 changes: 1 addition & 1 deletion catalogs/addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ builder.defineCatalogHandler((args) => {
staleRevalidate: STALE_REVALIDATE_AGE,
staleError: STALE_ERROR_AGE
}))
.catch(error => Promise.reject(`Failed retrieving catalog ${args.id}: ${error.message}`));
.catch(error => Promise.reject(`Failed retrieving catalog ${args.id}: ${error.message || error}`));
})

async function getCursor(catalog, providers, genre, offset) {
Expand Down
44 changes: 13 additions & 31 deletions catalogs/lib/cache.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,28 @@
import cacheManager from 'cache-manager';
import mangodbStore from 'cache-manager-mongodb';
import KeyvMongo from "@keyv/mongo";

const CATALOG_TTL = process.env.STREAM_TTL || 24 * 60 * 60; // 24 hours
const CATALOG_TTL = 24 * 60 * 60 * 1000; // 24 hours

const MONGO_URI = process.env.MONGODB_URI;

const remoteCache = initiateRemoteCache();
const remoteCache = MONGO_URI && new KeyvMongo(MONGO_URI, { collection: 'torrentio_catalog_collection' });

function initiateRemoteCache() {
if (MONGO_URI) {
return cacheManager.caching({
store: mangodbStore,
uri: MONGO_URI,
options: {
collection: 'torrentio_catalog_collection',
socketTimeoutMS: 120000,
useNewUrlParser: true,
useUnifiedTopology: false,
ttl: CATALOG_TTL
},
ttl: CATALOG_TTL,
ignoreCacheErrors: true
});
} else {
return cacheManager.caching({
store: 'memory',
ttl: CATALOG_TTL
});
}
}

function cacheWrap(cache, key, method, options) {
async function cacheWrap(cache, key, method, ttl) {
if (!cache) {
return method();
}
return cache.wrap(key, method, options);
const value = await remoteCache.get(key);
if (value !== undefined) {
return value;
}
const result = await method();
await remoteCache.set(key, result, ttl);
return result;
}

export function cacheWrapCatalog(key, method) {
return cacheWrap(remoteCache, key, method, { ttl: CATALOG_TTL });
return cacheWrap(remoteCache, key, method, CATALOG_TTL);
}

export function cacheWrapIds(key, method) {
return cacheWrap(remoteCache, `ids|${key}`, method, { ttl: CATALOG_TTL });
return cacheWrap(remoteCache, `ids|${key}`, method, CATALOG_TTL);
}
Loading

0 comments on commit 0cd43f1

Please sign in to comment.