-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharedInFlight.ts
51 lines (41 loc) · 1.69 KB
/
sharedInFlight.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
import {CacheLoader, CacheProvider} from './cacheProvider';
/**
* A cache that shares in-flight requests between callers.
*
* The scenario target by this cache is as follows:
* - Caller 1 requests the entry key A.
* - While the underlying cache is processing that request, possibly waiting
* for IO, a caller 2 requests the entry for the same key A.
* - This cache will return a reference to the same Promise returned to caller 1.
*
* Wrapping a cache with a `SharedInFlightCache` ensures that the same entry
* is not created twice. In the example above, if the entry was not present in
* the cache the loader would be called twice and, if the cache auto-saves,
* the cache entry would also be written twice.
* With this wrapper, the loaders would only be called once, and the entry
* would also be saved once, if the cache auto-saves.
*/
export class SharedInFlightCache<T> implements CacheProvider<string, T> {
private readonly inner: CacheProvider<string, T>;
private readonly pending = new Map<string, Promise<T>>();
public constructor(inner: CacheProvider<string, T>) {
this.inner = inner;
}
public get(key: string, loader: CacheLoader<string, T>): Promise<T> {
const pending = this.pending.get(key);
if (pending !== undefined) {
return pending;
}
const promise = this.inner
.get(key, loader)
.finally(() => this.pending.delete(key));
this.pending.set(key, promise);
return promise;
}
public set(key: string, value: T): Promise<void> {
return this.inner.set(key, value);
}
public delete(key: string): Promise<void> {
return this.inner.delete(key);
}
}