persistent.ts
3.23 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { createStorage } from '/@/utils/cache';
import { isIeFn } from '/@/utils/browser';
import { BASE_LOCAL_CACHE_KEY, BASE_SESSION_CACHE_KEY } from '/@/enums/cacheEnum';
const ls = createStorage(localStorage);
const ss = createStorage();
interface CacheStore {
local: Recordable;
session: Recordable;
}
/**
* @description: Persistent cache
*/
const cacheStore: CacheStore = {
// localstorage cache
local: {},
// sessionstorage cache
session: {},
};
function initCache() {
cacheStore.local = ls.get(BASE_LOCAL_CACHE_KEY) || {};
cacheStore.session = ss.get(BASE_SESSION_CACHE_KEY) || {};
}
initCache();
export function setLocal(key: string, value: any, immediate = false) {
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] } || {};
cacheStore.local[BASE_LOCAL_CACHE_KEY][key] = value;
if (immediate) {
ls.set(BASE_LOCAL_CACHE_KEY, cacheStore.local);
}
}
export function getLocal<T>(key: string): T | null {
try {
return cacheStore.local[BASE_LOCAL_CACHE_KEY][key];
} catch (error) {
return null;
}
}
export function removeLocal(key: string) {
if (cacheStore.local[BASE_LOCAL_CACHE_KEY]) {
Reflect.deleteProperty(cacheStore.local[BASE_LOCAL_CACHE_KEY], key);
}
}
export function clearLocal(immediate = false) {
cacheStore.local = {};
immediate && ls.remove(BASE_LOCAL_CACHE_KEY);
}
export function setSession(key: string, value: any, immediate = false) {
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] } || {};
cacheStore.session[BASE_SESSION_CACHE_KEY][key] = value;
if (immediate) {
ss.set(BASE_SESSION_CACHE_KEY, cacheStore.session);
}
}
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;
}
}
export function clearSession(immediate = false) {
cacheStore.session = {};
immediate && ss.remove(BASE_SESSION_CACHE_KEY);
}
export function clearAll() {
clearLocal();
clearSession();
}
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);
}
(() => {
// /** Write to local before closing window */
window.addEventListener('beforeunload', () => {
persistentCache();
});
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();
}
}
}
if (isIeFn() && (document as any).attachEvent) {
(document as any).attachEvent('onstorage', storageChange);
} else {
window.addEventListener('storage', storageChange);
}
})();