-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefixed.ts
More file actions
27 lines (21 loc) · 807 Bytes
/
Copy pathprefixed.ts
File metadata and controls
27 lines (21 loc) · 807 Bytes
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
import type {CacheProvider} from './cacheProvider';
/**
* A cache that prefixes keys with a string.
*/
export class PrefixedCache<V> implements CacheProvider<string, V> {
private readonly inner: CacheProvider<string, V>;
private readonly prefix: string;
public constructor(inner: CacheProvider<string, V>, prefix: string) {
this.inner = inner;
this.prefix = prefix;
}
public get(key: string, loader: (key: string) => Promise<V>): Promise<V> {
return this.inner.get(`${this.prefix}/${key}`, () => loader(key));
}
public set(key: string, value: V): Promise<void> {
return this.inner.set(`${this.prefix}/${key}`, value);
}
public delete(key: string): Promise<void> {
return this.inner.delete(`${this.prefix}/${key}`);
}
}