This commit is contained in:
2025-07-26 13:44:32 +01:00
parent 5b584a90a5
commit 2d02acacc7
24 changed files with 4519 additions and 77 deletions

26
src_dataset/cache.mjs Normal file
View File

@@ -0,0 +1,26 @@
import { existsSync } from "node:fs";
import { readFile, writeFile } from "node:fs/promises";
import { resolve } from "node:path";
/**
*
* @template T
* @param {string} fileName
* @param {()=>Promise<T>} asyncCallback
* @returns {Promise<T>}
*/
export async function cacheFunctionOutput(fileName, asyncCallback) {
const fileLoc = resolve('./cache', fileName);
if (existsSync(fileLoc)) {
console.log("[cacher] Using cached ", fileLoc);
const fileContents = (await readFile(fileLoc)).toString();
return JSON.parse(fileContents);
} else {
console.log("[cacher] cache miss")
const returnRes = await asyncCallback();
const fileContents = JSON.stringify(returnRes);
await writeFile(fileLoc,fileContents);
console.log("[cacher] saved ",fileLoc)
return returnRes;
}
}