提交 | 用户 | 时间
|
fd2207
|
1 |
const sessionCache = { |
懒 |
2 |
set (key, value) { |
|
3 |
if (!sessionStorage) { |
|
4 |
return |
|
5 |
} |
|
6 |
if (key != null && value != null) { |
|
7 |
sessionStorage.setItem(key, value) |
|
8 |
} |
|
9 |
}, |
|
10 |
get (key) { |
|
11 |
if (!sessionStorage) { |
|
12 |
return null |
|
13 |
} |
|
14 |
if (key == null) { |
|
15 |
return null |
|
16 |
} |
|
17 |
return sessionStorage.getItem(key) |
|
18 |
}, |
|
19 |
setJSON (key, jsonValue) { |
|
20 |
if (jsonValue != null) { |
|
21 |
this.set(key, JSON.stringify(jsonValue)) |
|
22 |
} |
|
23 |
}, |
|
24 |
getJSON (key) { |
|
25 |
const value = this.get(key) |
|
26 |
if (value != null) { |
|
27 |
return JSON.parse(value) |
|
28 |
} |
|
29 |
}, |
|
30 |
remove (key) { |
|
31 |
sessionStorage.removeItem(key); |
|
32 |
} |
|
33 |
} |
|
34 |
const localCache = { |
|
35 |
set (key, value) { |
|
36 |
if (!localStorage) { |
|
37 |
return |
|
38 |
} |
|
39 |
if (key != null && value != null) { |
|
40 |
localStorage.setItem(key, value) |
|
41 |
} |
|
42 |
}, |
|
43 |
get (key) { |
|
44 |
if (!localStorage) { |
|
45 |
return null |
|
46 |
} |
|
47 |
if (key == null) { |
|
48 |
return null |
|
49 |
} |
|
50 |
return localStorage.getItem(key) |
|
51 |
}, |
|
52 |
setJSON (key, jsonValue) { |
|
53 |
if (jsonValue != null) { |
|
54 |
this.set(key, JSON.stringify(jsonValue)) |
|
55 |
} |
|
56 |
}, |
|
57 |
getJSON (key) { |
|
58 |
const value = this.get(key) |
|
59 |
if (value != null) { |
|
60 |
return JSON.parse(value) |
|
61 |
} |
|
62 |
}, |
|
63 |
remove (key) { |
|
64 |
localStorage.removeItem(key); |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
export default { |
|
69 |
/** |
|
70 |
* 会话级缓存 |
|
71 |
*/ |
|
72 |
session: sessionCache, |
|
73 |
/** |
|
74 |
* 本地缓存 |
|
75 |
*/ |
|
76 |
local: localCache |
|
77 |
} |