vben
authored
5 years ago
1
import { createStorage } from '/@/utils/cache';
2
3
4
5
6
7
8
import { isIeFn } from '/@/utils/browser';
import { BASE_LOCAL_CACHE_KEY, BASE_SESSION_CACHE_KEY } from '/@/enums/cacheEnum';
const ls = createStorage(localStorage);
const ss = createStorage();
vben
authored
5 years ago
9
interface CacheStore {
vben
authored
5 years ago
10
11
local: Recordable;
session: Recordable;
vben
authored
5 years ago
12
13
}
14
15
16
/**
* @description: Persistent cache
*/
vben
authored
5 years ago
17
const cacheStore: CacheStore = {
18
19
20
21
22
23
24
25
26
27
// localstorage cache
local: {},
// sessionstorage cache
session: {},
};
function initCache() {
cacheStore.local = ls.get(BASE_LOCAL_CACHE_KEY) || {};
cacheStore.session = ss.get(BASE_SESSION_CACHE_KEY) || {};
}
vben
authored
5 years ago
28
29
30
initCache();
vben
authored
5 years ago
31
export function setLocal(key: string, value: any, immediate = false) {
vben
authored
5 years ago
32
33
34
35
const local = ls.get(BASE_LOCAL_CACHE_KEY)?.[BASE_LOCAL_CACHE_KEY] || {};
cacheStore.local[BASE_LOCAL_CACHE_KEY] =
{ ...local, ...cacheStore.local[BASE_LOCAL_CACHE_KEY] } || {};
36
cacheStore.local[BASE_LOCAL_CACHE_KEY][key] = value;
vben
authored
5 years ago
37
vben
authored
5 years ago
38
if (immediate) {
vben
authored
5 years ago
39
ls.set(BASE_LOCAL_CACHE_KEY, cacheStore.local);
vben
authored
5 years ago
40
}
41
42
43
44
45
46
47
48
49
}
export function getLocal<T>(key: string): T | null {
try {
return cacheStore.local[BASE_LOCAL_CACHE_KEY][key];
} catch (error) {
return null;
}
}
vben
authored
5 years ago
50
51
52
53
54
55
56
export function removeLocal(key: string) {
if (cacheStore.local[BASE_LOCAL_CACHE_KEY]) {
Reflect.deleteProperty(cacheStore.local[BASE_LOCAL_CACHE_KEY], key);
}
}
vben
authored
5 years ago
57
export function clearLocal(immediate = false) {
58
cacheStore.local = {};
vben
authored
5 years ago
59
immediate && ls.remove(BASE_LOCAL_CACHE_KEY);
60
61
}
vben
authored
5 years ago
62
export function setSession(key: string, value: any, immediate = false) {
vben
authored
5 years ago
63
64
65
66
67
const session = ss.get(BASE_SESSION_CACHE_KEY)?.[BASE_SESSION_CACHE_KEY] || {};
cacheStore.session[BASE_SESSION_CACHE_KEY] =
{ ...session, ...cacheStore.session[BASE_SESSION_CACHE_KEY] } || {};
68
cacheStore.session[BASE_SESSION_CACHE_KEY][key] = value;
vben
authored
5 years ago
69
vben
authored
5 years ago
70
if (immediate) {
vben
authored
5 years ago
71
ss.set(BASE_SESSION_CACHE_KEY, cacheStore.session);
vben
authored
5 years ago
72
}
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}
export function removeSession(key: string) {
if (cacheStore.session[BASE_SESSION_CACHE_KEY]) {
Reflect.deleteProperty(cacheStore.session[BASE_SESSION_CACHE_KEY], key);
}
}
export function getSession<T>(key: string): T | null {
try {
return cacheStore.session[BASE_SESSION_CACHE_KEY][key];
} catch (error) {
return null;
}
}
vben
authored
5 years ago
89
export function clearSession(immediate = false) {
90
cacheStore.session = {};
vben
authored
5 years ago
91
immediate && ss.remove(BASE_SESSION_CACHE_KEY);
92
93
94
95
96
97
98
}
export function clearAll() {
clearLocal();
clearSession();
}
vben
authored
5 years ago
99
100
101
102
103
104
105
export function persistentCache() {
const localCache = cacheStore.local;
const sessionCache = cacheStore.session;
ls.set(BASE_LOCAL_CACHE_KEY, localCache);
ss.set(BASE_SESSION_CACHE_KEY, sessionCache);
}
106
107
108
(() => {
// /** Write to local before closing window */
window.addEventListener('beforeunload', () => {
vben
authored
5 years ago
109
persistentCache();
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
});
function storageChange(e: any) {
const { key, newValue, oldValue } = e;
if (!key) {
clearAll();
return;
}
if (!!newValue && !!oldValue) {
if (BASE_LOCAL_CACHE_KEY === key) {
clearLocal();
}
if (BASE_SESSION_CACHE_KEY === key) {
clearSession();
}
}
}
vben
authored
5 years ago
129
130
131
132
133
134
135
if (isIeFn() && (document as any).attachEvent) {
(document as any).attachEvent('onstorage', storageChange);
} else {
window.addEventListener('storage', storageChange);
}
})();