Refactor: continues to rewrite to TS

This commit is contained in:
SukkaW
2023-11-15 16:26:46 +08:00
parent ec338a659f
commit 99589cf2fc
32 changed files with 258 additions and 471 deletions

31
Build/lib/cache-apply.ts Normal file
View File

@@ -0,0 +1,31 @@
export const createCache = (namespace?: string, printStats = false) => {
const cache = new Map();
let hit = 0;
if (namespace && printStats) {
process.on('exit', () => {
console.log(`🔋 [cache] ${namespace} hit: ${hit}, size: ${cache.size}`);
});
}
return {
sync<T>(key: string, fn: () => T): T {
if (cache.has(key)) {
hit++;
return cache.get(key);
}
const value = fn();
cache.set(key, value);
return value;
},
async async<T>(key: string, fn: () => Promise<T>): Promise<T> {
if (cache.has(key)) {
hit++;
return cache.get(key);
}
const value = await fn();
cache.set(key, value);
return value;
}
};
};