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