-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathStoreCache.ts
More file actions
38 lines (32 loc) · 916 Bytes
/
StoreCache.ts
File metadata and controls
38 lines (32 loc) · 916 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
28
29
30
31
32
33
34
35
36
37
38
import IStoreWriter from './IStoreWriter';
import IStoreReader from './IStoreReader';
export default class StoreCache implements IStoreWriter, IStoreReader {
cache: { [key: number]: string }
writer: IStoreWriter;
reader: IStoreReader;
constructor(_writer: IStoreWriter, _reader: IStoreReader) {
this.cache = {}
this.writer = _writer
this.reader = _reader
}
public save(id: number, message: string): void {
this.writer.save(id, message)
this.addOrUpdate(id, message);
}
public read(id: number) {
if(this.exists(id)) {
return this.cache[id];
}
var retValue = this.reader.read(id);
if(retValue !== undefined) {
this.addOrUpdate(id, retValue);
}
return retValue;
}
private exists?(id: number): boolean {
return this.cache.hasOwnProperty(id);
}
private addOrUpdate(id: number, message: string) {
this.cache[id] = message;
}
}