-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.ts
More file actions
113 lines (93 loc) · 2.91 KB
/
Cache.ts
File metadata and controls
113 lines (93 loc) · 2.91 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import AsyncStorage from "@react-native-community/async-storage";
import RNFetchBlob from "rn-fetch-blob";
export const CACHE_DIR = `${RNFetchBlob.fs.dirs.CacheDir}/cache`;
export class Cache implements ICache {
constructor(callback: () => void) {
this.loadCache(() => {
callback();
});
// Cache saver
setInterval(() => {
AsyncStorage.setItem("cachedKeys", JSON.stringify(this.cachedKeys));
}, 10000);
this.clearOldCache();
}
public cachedKeys: Array<{ value: string; lastUsed: number; maxAge: number; type: "image" | "json" }> = [];
public cacheInitilized = false;
public loadCache(callback: () => void) {
AsyncStorage.getItem("cachedKeys", async (err, res) => {
if (err) return console.log(err);
this.cachedKeys = JSON.parse(res);
console.log("CCKEYs");
console.log(this.cachedKeys);
if (this.cachedKeys == null) {
this.cachedKeys = [];
AsyncStorage.setItem("cachedKeys", "[]");
}
let exists = await RNFetchBlob.fs.exists(CACHE_DIR);
if (!exists) RNFetchBlob.fs.mkdir(CACHE_DIR);
});
}
public getLastModified(key: string, callback: (lastModified: number) => void): void {
if (this.checkIfCached(key)) {
RNFetchBlob.fs.stat(`${CACHE_DIR}/${key}`).then(
(res) => {
callback(parseInt(res.lastModified));
},
() => {
callback(null);
},
);
} else {
callback(null);
}
}
public checkIfCached(key: string) {
if (!this.cachedKeys) return false;
return this.cachedKeys.find(({ value }) => key == value) !== undefined;
}
public async getCachedValue(key: string, callback: (error: any, value: string) => void) {
if (this.checkIfCached(key)) {
RNFetchBlob.fs
.readFile(`${CACHE_DIR}/${key}`, "utf8")
.then((file) => {
callback(null, file);
})
.catch((err) => {
callback(err, null);
});
let i = this.cachedKeys.findIndex(({ value }) => value == key);
this.cachedKeys[i].lastUsed = Math.floor(new Date().getTime() / 1000);
} else {
callback(new Error("Key is not cached"), null);
}
}
public addToCache(key: string, maxAge: number, type: "image" | "json") {
if (this.checkIfCached(key)) {
// let i = this.cachedKeys.findIndex(({ value }) => value == key);
// this.cachedKeys[i].lastUsed = Math.floor(new Date().getTime() / 1000);
return;
}
this.cachedKeys.push({ value: key, lastUsed: Math.floor(new Date().getTime() / 1000.0), maxAge, type });
}
public clearOldCache() {
this.cachedKeys = this.cachedKeys.reduce((pv, cv) => {
if (Math.floor(new Date().getTime() / 1000) - cv.maxAge > cv.lastUsed) {
RNFetchBlob.fs.unlink(`${CACHE_DIR}/${cv.value}`);
return pv;
}
return pv.concat(cv);
}, []);
}
public clearFromCache(key: string) {
let i = this.cachedKeys.findIndex(({ value }) => value == key);
this.cachedKeys.splice(i, 1);
RNFetchBlob.fs.unlink(`${CACHE_DIR}/${key}`);
}
}
export const MAX_AGE = {
day: 86400,
week: 604800,
month: 2629743,
year: 31556926,
};