allow bypassing cache by passing null for path
This commit is contained in:
parent
df8ef3eb6c
commit
fca6403201
3 changed files with 14 additions and 5 deletions
16
src/index.ts
16
src/index.ts
|
@ -5,14 +5,22 @@ import { serialize, deserialize } from 'v8';
|
|||
|
||||
type Mode = string | number | boolean | null | undefined;
|
||||
|
||||
export const autocache = (path: string, mode: Mode) => {
|
||||
path = resolve(path);
|
||||
export const autocache = (path: string | null, mode: Mode) => {
|
||||
if (path == null) {
|
||||
return {
|
||||
async cache(key: string, compute_value: () => Promise<any>) {
|
||||
return compute_value();
|
||||
},
|
||||
close() {},
|
||||
};
|
||||
}
|
||||
const resolved_path = resolve(path);
|
||||
let cache = new Map<Mode, Map<string, any>>();
|
||||
const all_entries = new Map<string, any>();
|
||||
const used_entries = new Map<string, any>();
|
||||
const pending_entries = new Map<string, Promise<any>>();
|
||||
try {
|
||||
const data = deserialize(readFileSync(path));
|
||||
const data = deserialize(readFileSync(resolved_path));
|
||||
if (data && typeof data === 'object' && data.schema === 2 && data.cache instanceof Map) {
|
||||
cache = data.cache;
|
||||
for (const values of cache.values()) {
|
||||
|
@ -42,7 +50,7 @@ export const autocache = (path: string, mode: Mode) => {
|
|||
return value;
|
||||
},
|
||||
close() {
|
||||
writeFileSync(path, serialize({ schema: 2, cache }));
|
||||
writeFileSync(resolved_path, serialize({ schema: 2, cache }));
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
"noUnusedLocals": true,
|
||||
"removeComments": true,
|
||||
"sourceMap": true,
|
||||
"strictNullChecks": true,
|
||||
"target": "esnext"
|
||||
},
|
||||
"include": ["src"]
|
||||
|
|
2
types/index.d.ts
vendored
2
types/index.d.ts
vendored
|
@ -1,5 +1,5 @@
|
|||
export function autocache(
|
||||
path: string,
|
||||
path: string | null,
|
||||
mode: string | number | boolean | null | undefined,
|
||||
): {
|
||||
cache: <T>(key: string, compute_value: () => T | Promise<T>) => Promise<T>;
|
||||
|
|
Loading…
Reference in a new issue