-
Notifications
You must be signed in to change notification settings - Fork 101
/
Cache.js
48 lines (41 loc) · 1.25 KB
/
Cache.js
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
// NOTE: This script was written by evandcoleman: https://github.com/evandcoleman/scriptable
class Cache {
constructor(name) {
this.fm = FileManager.iCloud();
this.cachePath = this.fm.joinPath(this.fm.documentsDirectory(), name);
if (!this.fm.fileExists(this.cachePath)) {
this.fm.createDirectory(this.cachePath);
}
}
async read(key, expirationMinutes) {
try {
const path = this.fm.joinPath(this.cachePath, key);
await this.fm.downloadFileFromiCloud(path);
const createdAt = this.fm.creationDate(path);
if (expirationMinutes) {
if ((new Date()) - createdAt > (expirationMinutes * 60000)) {
this.fm.remove(path);
return null;
}
}
const value = this.fm.readString(path);
try {
return JSON.parse(value);
} catch (error) {
return value;
}
} catch (error) {
return null;
}
}
write(key, value) {
const path = this.fm.joinPath(this.cachePath, key.replace('/', '-'));
console.log(`Caching to ${path}...`);
if (typeof value === 'string' || value instanceof String) {
this.fm.writeString(path, value);
} else {
this.fm.writeString(path, JSON.stringify(value));
}
}
}
module.exports = Cache;