[update]
This commit is contained in:
26
src_dataset/cache.mjs
Normal file
26
src_dataset/cache.mjs
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user