torrentio-scraper/catalogs/lib/cache.js
2024-11-16 22:21:49 +02:00

28 lines
752 B
JavaScript

import KeyvMongo from "@keyv/mongo";
const CATALOG_TTL = 24 * 60 * 60 * 1000; // 24 hours
const MONGO_URI = process.env.MONGODB_URI;
const remoteCache = MONGO_URI && new KeyvMongo(MONGO_URI, { collection: 'torrentio_catalog_collection' });
async function cacheWrap(cache, key, method, ttl) {
if (!cache) {
return method();
}
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, CATALOG_TTL);
}
export function cacheWrapIds(key, method) {
return cacheWrap(remoteCache, `ids|${key}`, method, CATALOG_TTL);
}