-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonSerializingCache.ts
More file actions
53 lines (42 loc) · 1.45 KB
/
NonSerializingCache.ts
File metadata and controls
53 lines (42 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { ICache } from './ICache';
import Debug from 'debug';
const debug = Debug('layer-cache:nonserializing');
type FillFunction = (key: string) => Promise<string>
type NonSerializingCacheConstructor = new (cache: ICache, fillFn: FillFunction) => ICache;
class NonSerializingCacheImpl implements ICache {
cache: ICache
fillFn: FillFunction
constructor(cache: ICache, fillFn: FillFunction) {
this.cache = cache;
this.fillFn = fillFn;
}
async get(key: string): Promise<string> {
debug("Getting '%s'", key);
let result = await this.cache.get(key);
if (result === undefined) {
try {
result = await this.fillFn(key);
} catch (err) {
debug("Fill '%s' failed", key);
throw err;
}
try {
await this.cache.set(key, result);
debug("Storing '%s' successful", key);
} catch (err) {
debug("Storing '%s' failed: %s", key);
throw err;
}
}
debug("Get '%s' successful", key);
return result;
}
async set(key: string, value: string): Promise<void> {
throw new Error("Set is not implemented")
}
del(key: string): Promise<void> {
debug("Deleting '%s'", key);
return this.cache.del(key);
}
}
export const NonSerializingCache: NonSerializingCacheConstructor = NonSerializingCacheImpl;