mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2026-01-31 14:51:09 +03:00
Add disk caching for youtube api calls
Fixes https://github.com/ajayyy/SponsorBlockServer/issues/239
This commit is contained in:
@@ -75,7 +75,8 @@ addDefaults(config, {
|
||||
{
|
||||
name: "vipUsers"
|
||||
}]
|
||||
}
|
||||
},
|
||||
diskCache: null
|
||||
});
|
||||
|
||||
// Add defaults
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { PoolConfig } from 'pg';
|
||||
import * as redis from 'redis';
|
||||
import { CacheOptions } from "@ajayyy/lru-diskcache";
|
||||
|
||||
export interface SBSConfig {
|
||||
port: number;
|
||||
@@ -41,6 +42,7 @@ export interface SBSConfig {
|
||||
maxRewardTimePerSegmentInSeconds?: number;
|
||||
postgres?: PoolConfig;
|
||||
dumpDatabase?: DumpDatabase;
|
||||
diskCache: CacheOptions;
|
||||
}
|
||||
|
||||
export interface WebhookConfig {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Better ecord that will work with branded types
|
||||
* Better record that will work with branded types
|
||||
* Keys still don't work properly though and are always string
|
||||
*/
|
||||
export type SBRecord<K extends string, T> = {
|
||||
|
||||
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