Add disk caching for youtube api calls

Fixes https://github.com/ajayyy/SponsorBlockServer/issues/239
This commit is contained in:
Ajay Ramachandran
2021-06-26 23:21:51 -04:00
parent e1cf360825
commit 07ab48da1f
8 changed files with 307 additions and 53 deletions

32
src/utils/diskCache.ts Normal file
View 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;

View File

@@ -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 {