mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-17 03:44:20 +03:00
Add disk caching for youtube api calls
Fixes https://github.com/ajayyy/SponsorBlockServer/issues/239
This commit is contained in:
32
src/utils/diskCache.ts
Normal file
32
src/utils/diskCache.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import LRU from "@ajayyy/lru-diskcache";
|
||||
import { config } from "../config";
|
||||
|
||||
let DiskCache: LRU<string, string>;
|
||||
|
||||
if (config.diskCache) {
|
||||
DiskCache = new LRU('./databases/cache', config.diskCache);
|
||||
DiskCache.init();
|
||||
} else {
|
||||
DiskCache = {
|
||||
// constructor(rootPath, options): {};
|
||||
|
||||
init() {},
|
||||
|
||||
reset() {},
|
||||
|
||||
has(key) { return false; },
|
||||
|
||||
get(key, opts) { return null; },
|
||||
|
||||
// Returns size
|
||||
set(key, dataOrSteam) { return new Promise((resolve) => 0); },
|
||||
|
||||
del(key) {},
|
||||
|
||||
size() { return 0; },
|
||||
|
||||
prune() {},
|
||||
};
|
||||
}
|
||||
|
||||
export default DiskCache;
|
||||
@@ -1,8 +1,8 @@
|
||||
import fetch from 'node-fetch';
|
||||
import {config} from '../config';
|
||||
import {Logger} from './logger';
|
||||
import redis from './redis';
|
||||
import { APIVideoData, APIVideoInfo } from '../types/youtubeApi.model';
|
||||
import DiskCache from './diskCache';
|
||||
|
||||
export class YouTubeAPI {
|
||||
static async listVideos(videoID: string, ignoreCache = false): Promise<APIVideoInfo> {
|
||||
@@ -10,14 +10,17 @@ export class YouTubeAPI {
|
||||
return { err: "Invalid video ID" };
|
||||
}
|
||||
|
||||
const redisKey = "yt.newleaf.video." + videoID;
|
||||
const cacheKey = "yt.newleaf.video." + videoID;
|
||||
if (!ignoreCache) {
|
||||
const {err, reply} = await redis.getAsync(redisKey);
|
||||
try {
|
||||
const data = await DiskCache.get(cacheKey);
|
||||
|
||||
if (!err && reply) {
|
||||
Logger.debug("redis: no cache for video information: " + videoID);
|
||||
|
||||
return { err: err?.message, data: JSON.parse(reply) }
|
||||
if (data) {
|
||||
Logger.debug("YouTube API: cache used for video information: " + videoID);
|
||||
return { err: null, data: JSON.parse(data) }
|
||||
}
|
||||
} catch (err) {
|
||||
return { err }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,13 +36,9 @@ export class YouTubeAPI {
|
||||
return { err: data.error, data: null };
|
||||
}
|
||||
|
||||
redis.setAsync(redisKey, JSON.stringify(data)).then((result) => {
|
||||
if (result?.err) {
|
||||
Logger.warn(result?.err.message);
|
||||
} else {
|
||||
Logger.debug("redis: video information cache set for: " + videoID);
|
||||
}
|
||||
});
|
||||
DiskCache.set(cacheKey, JSON.stringify(data))
|
||||
.catch((err) => Logger.warn(err))
|
||||
.then(() => Logger.debug("YouTube API: video information cache set for: " + videoID));
|
||||
|
||||
return { err: false, data };
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user