Blame view

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

window.addEventListener('storage', storageChange);
112
113
initPersistentMemory();