-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathMessageStore.ts
More file actions
52 lines (48 loc) · 1.48 KB
/
MessageStore.ts
File metadata and controls
52 lines (48 loc) · 1.48 KB
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
42
43
44
45
46
47
48
49
50
51
52
import FileStore from "./FileStore";
import StoreCache from "./StoreCache";
import StoreLogger from "./StoreLogger";
export default class MessageStore {
filestore : FileStore;
cache: StoreCache;
logger: StoreLogger;
constructor(directory: string) {
this.logger = new StoreLogger();
this.filestore = new FileStore(directory, this.logger);
this.cache = new StoreCache(this.logger);
}
/**
*
* @param id the id of the file to save
* @param message the text message to write to the file
*
* Function writes the file to disk using the id as part
* of the filename. The id is a number and the file name is
* formed as a .txt file using the pattern id.txt. Its saved
* in the relative directory as set in the constructor.
*/
public async save (id: number, message: string) {
await this.filestore.save(id, message);
this.cache.addOrUpdate(id, message);
}
/**
*
* @param id the id of the file to read
*
* Function checks if the file exists and
* if not returns an empty string.
* If the file does exist then the function
* checks if the file id is in the cache and
* if not will read the contents of the file
* from disk and add to the cache.
*
* @returns message string
*/
public read(id: number): string {
if(!this.cache.exists(id)) {
// Does not exist in cache so read from file
var message = this.filestore.read(id)
this.cache.getOrAdd(id, message)
}
return this.cache.getOrAdd(id)
}
}