update cache manager usage

This commit is contained in:
TheBeastLT 2024-11-16 22:21:49 +02:00
parent 0a45dde099
commit 0cd43f1f56
4 changed files with 802 additions and 715 deletions

View file

@ -41,7 +41,7 @@ builder.defineCatalogHandler((args) => {
staleRevalidate: STALE_REVALIDATE_AGE, staleRevalidate: STALE_REVALIDATE_AGE,
staleError: STALE_ERROR_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) { async function getCursor(catalog, providers, genre, offset) {

View file

@ -1,46 +1,28 @@
import cacheManager from 'cache-manager'; import KeyvMongo from "@keyv/mongo";
import mangodbStore from 'cache-manager-mongodb';
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 MONGO_URI = process.env.MONGODB_URI;
const remoteCache = initiateRemoteCache(); const remoteCache = MONGO_URI && new KeyvMongo(MONGO_URI, { collection: 'torrentio_catalog_collection' });
function initiateRemoteCache() { async function cacheWrap(cache, key, method, ttl) {
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) {
if (!cache) { if (!cache) {
return method(); 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) { 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) { export function cacheWrapIds(key, method) {
return cacheWrap(remoteCache, `ids|${key}`, method, { ttl: CATALOG_TTL }); return cacheWrap(remoteCache, `ids|${key}`, method, CATALOG_TTL);
} }

File diff suppressed because it is too large Load diff

View file

@ -7,20 +7,19 @@
"start": "node index.js" "start": "node index.js"
}, },
"engines": { "engines": {
"node": "16.x" "node": ">=16.x"
}, },
"author": "TheBeastLT <pauliox@beyond.lt>", "author": "TheBeastLT <pauliox@beyond.lt>",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"axios": "^1.6.1", "@keyv/mongo": "^3.0.1",
"axios": "^1.7.7",
"bottleneck": "^2.19.5", "bottleneck": "^2.19.5",
"cache-manager": "^3.4.4", "moment": "^2.30.1",
"cache-manager-mongodb": "^0.3.0", "pg": "^8.13.1",
"moment": "^2.29.4",
"pg": "^8.8.0",
"pg-hstore": "^2.3.4", "pg-hstore": "^2.3.4",
"request-ip": "^3.3.0", "request-ip": "^3.3.0",
"sequelize": "^6.29.0", "sequelize": "^6.37.5",
"stremio-addon-sdk": "^1.6.10" "stremio-addon-sdk": "^1.6.10"
} }
} }