-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathStoreCache.ts
More file actions
34 lines (29 loc) · 899 Bytes
/
StoreCache.ts
File metadata and controls
34 lines (29 loc) · 899 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
import StoreLogger from './StoreLogger'
import IStoreCache from './IStoreCache'
import IStoreLogger from './IStoreLogger';
export default class StoreCache implements IStoreCache {
cache: any;
logger: IStoreLogger;
constructor(_logger: IStoreLogger) {
this.cache = {}
this.logger = _logger
}
public save(id: number, message: string): void {
this.cache[id] = message;
}
public getOrAdd(id: number, fnStoreRead: Function) {
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.save(id, message);
}
return this.cache[id];
}
public exists?(id: number): boolean {
return this.cache.hasOwnProperty(id);
}
}