-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.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
// cache.js
const Cache = {
// Cache duration in milliseconds (24 hours)
CACHE_DURATION: 24 * 60 * 60 * 1000,
async set(key, value) {
const item = {
value: value,
timestamp: Date.now()
};
return new Promise((resolve) => {
chrome.storage.local.set({ [key]: item }, () => resolve());
});
},
async get(key) {
return new Promise((resolve) => {
chrome.storage.local.get(key, (result) => {
const item = result[key];
if (!item) {
resolve(null);
return;
}
// Check if cache has expired
if (Date.now() - item.timestamp > this.CACHE_DURATION) {
this.remove(key);
resolve(null);
return;
}
resolve(item.value);
});
});
},
async remove(key) {
return new Promise((resolve) => {
chrome.storage.local.remove(key, () => resolve());
});
},
createKey(type, text, sourceLang, targetLang) {
return `${type}_${text}_${sourceLang}_${targetLang}`.toLowerCase();
}
};
window.DictionaryCache = Cache;