Blame view

src/utils/helper/persistent.ts 3.06 KB
1
import { createStorage } from '/@/utils/cache';
陈文彬 authored
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
}
陈文彬 authored
13
14
15
/**
 * @description:  Persistent cache
 */
vben authored
16
const cacheStore: CacheStore = {
陈文彬 authored
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) || {};
}
27
陈文彬 authored
28
29
initCache();
30
export function setLocal(key: string, value: any, immediate = false) {
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] } || {};
陈文彬 authored
35
  cacheStore.local[BASE_LOCAL_CACHE_KEY][key] = value;
36
37
  if (immediate) {
38
    ls.set(BASE_LOCAL_CACHE_KEY, cacheStore.local);
39
  }
陈文彬 authored
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;
  }
}
49
陈文彬 authored
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);
  }
}
56
export function clearLocal(immediate = false) {
陈文彬 authored
57
  cacheStore.local = {};
58
  immediate && ls.remove(BASE_LOCAL_CACHE_KEY);
陈文彬 authored
59
60
}
61
export function setSession(key: string, value: any, immediate = false) {
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] } || {};
陈文彬 authored
67
  cacheStore.session[BASE_SESSION_CACHE_KEY][key] = value;
68
69
  if (immediate) {
70
    ss.set(BASE_SESSION_CACHE_KEY, cacheStore.session);
71
  }
陈文彬 authored
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;
  }
}
88
export function clearSession(immediate = false) {
陈文彬 authored
89
  cacheStore.session = {};
90
  immediate && ss.remove(BASE_SESSION_CACHE_KEY);
陈文彬 authored
91
92
93
94
95
96
97
}

export function clearAll() {
  clearLocal();
  clearSession();
}
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);
}
陈文彬 authored
105
106
107
(() => {
  // /** Write to local before closing window */
  window.addEventListener('beforeunload', () => {
108
    persistentCache();
陈文彬 authored
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();
      }
    }
  }
128
129
  window.addEventListener('storage', storageChange);
陈文彬 authored
130
})();