-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.ts
41 lines (37 loc) · 1.22 KB
/
proxy.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
/**
* Defines an interface for an object storage system that can load objects by key.
*/
interface ObjectStorage {
load(key: string): string | null;
}
/**
* Provides a remote object storage implementation that loads objects from a remote server.
* TODO: This must be implemented
*/
class RemoteObjectStorage implements ObjectStorage {
constructor(private readonly key: string) {}
// Load from a remote server
load(key: string): string | null {
return `Image URL For: (${this.key})`;
}
}
/**
* Provides a caching proxy for an ObjectStorage implementation,
* loading objects from a remote storage and caching the results,
* instead of loading them from the remote storage every time.
*/
class CachedObjectStorage implements ObjectStorage {
private cached: Map<string, string | null> = new Map();
constructor(private readonly remote: ObjectStorage) {}
load(key: string): string | null {
// If the key is in the cache, return it
if (this.cached.has(key)) {
return this.cached.get(key)!;
}
// If the key is not in the cache, load it from the remote storage
const value = this.remote.load(key);
this.cached.set(key, value);
return value;
}
}
export { CachedObjectStorage, ObjectStorage };