mirror of
https://github.com/TheBeastLT/torrentio-scraper.git
synced 2026-03-11 17:15:37 +00:00
revert to mongo for streams cache
Some checks failed
Deploy Addon / build (SSH_HOST_2, SSH_KEY_2) (push) Has been cancelled
Deploy Addon / build (SSH_HOST_3, SSH_KEY_3) (push) Has been cancelled
Deploy Addon / build (SSH_HOST_4, SSH_KEY_4) (push) Has been cancelled
Deploy Addon / build (SSH_HOST_5, SSH_KEY_5) (push) Has been cancelled
Some checks failed
Deploy Addon / build (SSH_HOST_2, SSH_KEY_2) (push) Has been cancelled
Deploy Addon / build (SSH_HOST_3, SSH_KEY_3) (push) Has been cancelled
Deploy Addon / build (SSH_HOST_4, SSH_KEY_4) (push) Has been cancelled
Deploy Addon / build (SSH_HOST_5, SSH_KEY_5) (push) Has been cancelled
This commit is contained in:
parent
3061c31be7
commit
dd96e3e081
2 changed files with 11 additions and 13 deletions
2
.github/workflows/deploy_addon.yml
vendored
2
.github/workflows/deploy_addon.yml
vendored
|
|
@ -19,6 +19,8 @@ jobs:
|
||||||
ssh-key: SSH_KEY_3
|
ssh-key: SSH_KEY_3
|
||||||
- ssh-host: SSH_HOST_4
|
- ssh-host: SSH_HOST_4
|
||||||
ssh-key: SSH_KEY_4
|
ssh-key: SSH_KEY_4
|
||||||
|
- ssh-host: SSH_HOST_5
|
||||||
|
ssh-key: SSH_KEY_5
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import KeyvMongo from "@keyv/mongo";
|
import KeyvMongo from "@keyv/mongo";
|
||||||
import KeyvRedis from "@keyv/redis";
|
|
||||||
import { KeyvCacheableMemory } from "cacheable";
|
import { KeyvCacheableMemory } from "cacheable";
|
||||||
import { isStaticUrl } from '../moch/static.js';
|
import { isStaticUrl } from '../moch/static.js';
|
||||||
|
|
||||||
|
|
@ -15,10 +14,8 @@ const AVAILABILITY_TTL = 5 * 24 * 60 * 60 * 1000; // 5 days
|
||||||
const MESSAGE_VIDEO_URL_TTL = 60 * 1000; // 1 minutes
|
const MESSAGE_VIDEO_URL_TTL = 60 * 1000; // 1 minutes
|
||||||
|
|
||||||
const MONGO_URI = process.env.MONGODB_URI;
|
const MONGO_URI = process.env.MONGODB_URI;
|
||||||
const REDIS_URL = process.env.REDIS_URL;
|
|
||||||
|
|
||||||
const memoryCache = new KeyvCacheableMemory({ ttl: MESSAGE_VIDEO_URL_TTL, lruSize: Infinity });
|
const memoryCache = new KeyvCacheableMemory({ ttl: MESSAGE_VIDEO_URL_TTL, lruSize: Infinity });
|
||||||
const redisCache = REDIS_URL && new KeyvRedis(REDIS_URL);
|
|
||||||
const mongoCache = MONGO_URI && new KeyvMongo(MONGO_URI, {
|
const mongoCache = MONGO_URI && new KeyvMongo(MONGO_URI, {
|
||||||
collection: 'torrentio_addon_collection',
|
collection: 'torrentio_addon_collection',
|
||||||
minPoolSize: 50,
|
minPoolSize: 50,
|
||||||
|
|
@ -26,29 +23,28 @@ const mongoCache = MONGO_URI && new KeyvMongo(MONGO_URI, {
|
||||||
maxConnecting: 5,
|
maxConnecting: 5,
|
||||||
});
|
});
|
||||||
|
|
||||||
async function cacheWrapRedis(key, method, ttl) {
|
async function cacheWrap(cache, key, method, ttl) {
|
||||||
const value = await redisCache.get(key);
|
if (!cache) {
|
||||||
|
return method();
|
||||||
|
}
|
||||||
|
const value = await cache.get(key);
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
try {
|
return value;
|
||||||
return JSON.parse(value);
|
|
||||||
} catch (e) {
|
|
||||||
console.warn(`Cache parse error for key ${key}`, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const result = await method();
|
const result = await method();
|
||||||
const ttlValue = ttl instanceof Function ? ttl(result) : ttl;
|
const ttlValue = ttl instanceof Function ? ttl(result) : ttl;
|
||||||
await redisCache.set(key, JSON.stringify(result), ttlValue);
|
await cache.set(key, result, ttlValue);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function cacheWrapStream(id, method) {
|
export function cacheWrapStream(id, method) {
|
||||||
const ttl = (streams) => streams.length ? STREAM_TTL : STREAM_EMPTY_TTL;
|
const ttl = (streams) => streams.length ? STREAM_TTL : STREAM_EMPTY_TTL;
|
||||||
return cacheWrapRedis(`${STREAM_KEY_PREFIX}:${id}`, method, ttl);
|
return cacheWrap(mongoCache, `${STREAM_KEY_PREFIX}:${id}`, method, ttl);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function cacheWrapResolvedUrl(id, method) {
|
export function cacheWrapResolvedUrl(id, method) {
|
||||||
const ttl = (url) => isStaticUrl(url) ? MESSAGE_VIDEO_URL_TTL : RESOLVED_URL_TTL;
|
const ttl = (url) => isStaticUrl(url) ? MESSAGE_VIDEO_URL_TTL : RESOLVED_URL_TTL;
|
||||||
return cacheWrapRedis(`${RESOLVED_URL_KEY_PREFIX}:${id}`, method, ttl);
|
return cacheWrap(mongoCache, `${RESOLVED_URL_KEY_PREFIX}:${id}`, method, ttl);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function cacheAvailabilityResults(infoHash, fileIds) {
|
export function cacheAvailabilityResults(infoHash, fileIds) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue