Blame view

src/utils/cache/persistent.ts 2.69 KB
1
import { createLocalStorage, createSessionStorage } from '/@/utils/cache';
Vben authored
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { Memory } from './memory';
import {
  TOKEN_KEY,
  USER_INFO_KEY,
  ROLES_KEY,
  LOCK_INFO_KEY,
  PROJ_CFG_KEY,
  APP_LOCAL_CACHE_KEY,
  APP_SESSION_CACHE_KEY,
} from '/@/enums/cacheEnum';
import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
import { toRaw } from 'vue';

interface BasicStore {
  [TOKEN_KEY]: string | number | null | undefined;
  [USER_INFO_KEY]: Recordable;
  [ROLES_KEY]: Recordable;
  [LOCK_INFO_KEY]: Recordable;
  [PROJ_CFG_KEY]: Recordable;
vben authored
21
22
}
Vben authored
23
type LocalStore = BasicStore;
24
Vben authored
25
type SessionStore = BasicStore;
陈文彬 authored
26
Vben authored
27
28
29
export type BasicKeys = keyof BasicStore;
type LocalKeys = keyof LocalStore;
type SessionKeys = keyof SessionStore;
30
31
32
const ls = createLocalStorage();
const ss = createSessionStorage();
33
Vben authored
34
35
const localMemory = new Memory(DEFAULT_CACHE_TIME);
const sessionMemory = new Memory(DEFAULT_CACHE_TIME);
陈文彬 authored
36
37
function initPersistentMemory() {
Vben authored
38
39
40
41
  const localCache = ls.get(APP_LOCAL_CACHE_KEY);
  const sessionCache = ls.get(APP_SESSION_CACHE_KEY);
  localCache && localMemory.resetCache(localCache);
  sessionCache && sessionMemory.resetCache(sessionCache);
陈文彬 authored
42
}
43
Vben authored
44
45
46
export class Persistent {
  static getLocal<T>(key: LocalKeys) {
    return localMemory.get(key)?.value as Nullable<T>;
陈文彬 authored
47
  }
48
Vben authored
49
50
51
52
  static setLocal(key: LocalKeys, value: LocalStore[LocalKeys], immediate = false): void {
    localMemory.set(key, toRaw(value));
    immediate && ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache);
  }
53
Vben authored
54
55
56
  static removeLocal(key: LocalKeys): void {
    localMemory.remove(key);
  }
57
Vben authored
58
59
  static clearLocal(): void {
    localMemory.clear();
60
  }
陈文彬 authored
61
Vben authored
62
63
  static getSession<T>(key: SessionKeys) {
    return sessionMemory.get(key)?.value as Nullable<T>;
陈文彬 authored
64
65
  }
Vben authored
66
67
68
  static setSession(key: SessionKeys, value: SessionStore[SessionKeys], immediate = false): void {
    sessionMemory.set(key, toRaw(value));
    immediate && ss.set(APP_SESSION_CACHE_KEY, localMemory);
陈文彬 authored
69
70
  }
Vben authored
71
72
73
74
75
76
  static removeSession(key: SessionKeys): void {
    sessionMemory.remove(key);
  }
  static clearSession(): void {
    sessionMemory.clear();
  }
陈文彬 authored
77
Vben authored
78
79
80
81
  static clearAll() {
    sessionMemory.clear();
    localMemory.clear();
  }
陈文彬 authored
82
83
}
Vben authored
84
85
86
87
window.addEventListener('beforeunload', function () {
  ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache);
  ss.set(APP_SESSION_CACHE_KEY, sessionMemory.getCache);
});
88
Vben authored
89
90
function storageChange(e: any) {
  const { key, newValue, oldValue } = e;
陈文彬 authored
91
Vben authored
92
93
94
95
  if (!key) {
    Persistent.clearAll();
    return;
  }
陈文彬 authored
96
Vben authored
97
98
99
  if (!!newValue && !!oldValue) {
    if (APP_LOCAL_CACHE_KEY === key) {
      Persistent.clearLocal();
陈文彬 authored
100
    }
Vben authored
101
102
    if (APP_SESSION_CACHE_KEY === key) {
      Persistent.clearSession();
陈文彬 authored
103
104
    }
  }
Vben authored
105
106
107
}

window.addEventListener('storage', storageChange);
108
109
initPersistentMemory();