-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ContentfulCache.js
50 lines (38 loc) · 765 Bytes
/
ContentfulCache.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
49
50
import { parse } from 'flatted'
export default class ContentfulCache {
constructor(cache) {
this.cache = new Map(cache);
}
clear() {
this.cache.clear();
return this;
}
extract() {
return Array.from(this.cache.entries());
}
has(key) {
return this.cache.has(key);
}
read(key) {
return this.cache.get(key);
}
restore(cache) {
if (!cache) {
return this;
}
if (typeof cache === 'string') {
const data = parse(cache);
if (Symbol.iterator in Object(data)) {
this.cache = new Map(data);
}
}
else if (Array.isArray(cache)) {
this.cache = new Map(cache);
}
return this;
}
write(key, value) {
this.cache.set(key, value);
return this;
}
}