-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathStoreCache.ts
More file actions
32 lines (27 loc) · 800 Bytes
/
StoreCache.ts
File metadata and controls
32 lines (27 loc) · 800 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
import StoreLogger from './StoreLogger'
export default class StoreCache {
cache: any;
logger: StoreLogger;
constructor(_logger: StoreLogger) {
this.cache = {}
this.logger = _logger
}
public addOrUpdate(id: number, message: string): void {
this.cache[id] = message;
}
public getOrAdd(id: number, fnStoreRead: any) {
this.logger.readingCache(id)
if(!this.exists(id)) {
// The message does not exist in the cache
this.logger.missingFromCache(id);
// Load the contents from the store using the passed in function
var message = fnStoreRead()
// Save the message to the cache
this.addOrUpdate(id, message);
}
return this.cache[id];
}
public exists?(id: number): boolean {
return this.cache.hasOwnProperty(id);
}
}