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

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