-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a45dde
commit 0cd43f1
Showing
4 changed files
with
802 additions
and
715 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.