Blame view

src/utils/cache/persistent.ts 2.93 KB
Vben authored
1
2
import type { LockInfo, UserInfo } from '/#/store';
import type { ProjectConfig } from '/#/config';
3
import type { RouteLocationNormalized } from 'vue-router';
4
5
import { createLocalStorage, createSessionStorage } from '/@/utils/cache';
Vben authored
6
7
8
9
10
11
12
13
14
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,
15
  MULTIPLE_TABS_KEY,
Vben authored
16
17
18
19
20
21
} from '/@/enums/cacheEnum';
import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
import { toRaw } from 'vue';

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

window.addEventListener('storage', storageChange);
114
115
initPersistentMemory();