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