Add disk cache service

Fixes #471
This commit is contained in:
Ajay
2022-05-17 02:29:36 -04:00
parent e79a8417f4
commit c9a0fb7bc3
6 changed files with 62 additions and 293 deletions

View File

@@ -112,9 +112,7 @@ addDefaults(config, {
name: "ratings"
}]
},
diskCache: {
max: 10737418240
},
diskCacheURL: null,
crons: null,
redis: {
enabled: false,

View File

@@ -1,6 +1,5 @@
import { PoolConfig } from "pg";
import * as redis from "redis";
import { CacheOptions } from "@ajayyy/lru-diskcache";
interface RedisConfig extends redis.RedisClientOptions {
enabled: boolean;
@@ -53,7 +52,7 @@ export interface SBSConfig {
maxRewardTimePerSegmentInSeconds?: number;
postgres?: CustomPostgresConfig;
dumpDatabase?: DumpDatabase;
diskCache: CacheOptions;
diskCacheURL: string;
crons: CronJobOptions;
}

View File

@@ -1,34 +1,41 @@
import LRU from "@ajayyy/lru-diskcache";
import axios, { AxiosError } from "axios";
import { config } from "../config";
import { Logger } from "./logger";
let DiskCache: LRU<string, string>;
class DiskCache {
async set(key: string, value: unknown): Promise<boolean> {
if (!config.diskCacheURL) return false;
if (config.diskCache) {
DiskCache = new LRU("./databases/cache", config.diskCache);
DiskCache.init();
} else {
DiskCache = {
/* eslint-disable @typescript-eslint/no-unused-vars */
// constructor(rootPath, options): {};
try {
const result = await axios.post(`${config.diskCacheURL}/api/v1/item`, {
key,
value
});
init(): void { return; },
return result.status === 200;
} catch (err) {
const response = (err as AxiosError).response;
if (!response || response.status !== 404) {
Logger.error(`DiskCache: Error setting key ${key}: ${err}`);
}
reset(): void { return; },
return false;
}
}
has(key: string): boolean { return false; },
async get(key: string): Promise<unknown> {
if (!config.diskCacheURL) return null;
get(key: string, opts?: {encoding?: string}): string { return null; },
try {
const result = await axios.get(`${config.diskCacheURL}/api/v1/item?key=${key}`, { timeout: 500 });
// Returns size
set(key: string, dataOrSteam: string): Promise<number> { return new Promise(() => 0); },
del(key: string): void { return; },
size(): number { return 0; },
prune(): void {return; },
/* eslint-enable @typescript-eslint/no-unused-vars */
};
return result.status === 200 ? result.data : null;
} catch (err) {
Logger.error(`DiskCache: Error getting key ${key}: ${err}`);
return null;
}
}
}
export default DiskCache;
const diskCache = new DiskCache();
export default diskCache;

View File

@@ -17,7 +17,7 @@ export class YouTubeAPI {
if (data) {
Logger.debug(`YouTube API: cache used for video information: ${videoID}`);
return { err: null, data: JSON.parse(data) };
return { err: null, data: data as APIVideoData };
}
} catch (err) {
return { err: err as string | boolean, data: null };
@@ -38,7 +38,7 @@ export class YouTubeAPI {
return { err: data.error, data: null };
}
const apiResult = data as APIVideoData;
DiskCache.set(cacheKey, JSON.stringify(apiResult))
DiskCache.set(cacheKey, apiResult)
.catch((err: any) => Logger.warn(err))
.then(() => Logger.debug(`YouTube API: video information cache set for: ${videoID}`));