-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
DefaultStrategy.ts
115 lines (99 loc) · 3.35 KB
/
DefaultStrategy.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { CacheClient, CacheStrategy, CacheStrategyContext } from '../interfaces';
export class DefaultStrategy implements CacheStrategy {
private pendingCacheRequestMap = new Map<string, Promise<any>>();
private pendingMethodCallMap = new Map<string, Promise<any>>();
private findCachedValue = async (client: CacheClient, key: string) => {
let cachedValue: any;
try {
if (this.pendingCacheRequestMap.has(key)) {
cachedValue = await this.pendingCacheRequestMap.get(key);
} else {
const cachePromise = client.get(key);
this.pendingCacheRequestMap.set(key, cachePromise);
cachedValue = await cachePromise;
}
} catch (err) {
throw err;
} finally {
this.pendingCacheRequestMap.delete(key);
}
return cachedValue;
};
async handle(context: CacheStrategyContext): Promise<any> {
try {
const cachedValue = await this.findCachedValue(context.client, context.key);
// If a value for the cacheKey was found in cache, simply return that.
if (cachedValue !== undefined && cachedValue !== null) {
return cachedValue;
}
} catch (err) {
if (context.fallbackClient) {
try {
const cachedValue = await this.findCachedValue(context.fallbackClient, context.key);
// If a value for the cacheKey was found in cache, simply return that.
if (cachedValue !== undefined && cachedValue !== null) {
return cachedValue;
}
} catch (fallbackErr) {}
}
if (context.debug) {
console.warn(
`type-cacheable Cacheable cache miss on method ${
context.originalMethod.name
} due to client error: ${(err as Error).message}`,
);
}
}
// On a cache miss, run the decorated method and cache its return value.
let result: any;
const pendingMethodRun = this.pendingMethodCallMap.get(context.key);
if (pendingMethodRun) {
result = await pendingMethodRun;
} else {
const methodPromise = new Promise(async (resolve, reject) => {
let returnValue;
let isCacheable = false;
try {
returnValue = await context.originalMethod!.apply(
context.originalMethodScope,
context.originalMethodArgs,
);
isCacheable = context.isCacheable(returnValue);
} catch (err) {
reject(err);
return;
}
if (!isCacheable) {
resolve(returnValue);
return;
}
try {
await context.client.set(context.key, returnValue, context.ttl);
} catch (err) {
if (context.fallbackClient) {
try {
await context.fallbackClient.set(context.key, returnValue, context.ttl);
} catch (err) {}
}
if (context.debug) {
console.warn(
`type-cacheable Cacheable set cache failure on method ${
context.originalMethod.name
} due to client error: ${(err as Error).message}`,
);
}
}
resolve(returnValue);
});
try {
this.pendingMethodCallMap.set(context.key, methodPromise);
result = await methodPromise;
} catch (err) {
throw err;
} finally {
this.pendingMethodCallMap.delete(context.key);
}
}
return result;
}
}