Vben
authored
|
1
2
|
import type { LockInfo, UserInfo } from '/@/store/types';
|
Vben
authored
|
3
4
|
import { ProjectConfig } from '/#/config';
|
Vben
authored
|
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;
|
Vben
authored
|
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;
|
vben
authored
|
28
|
|
Vben
authored
|
29
|
type SessionStore = BasicStore;
|
|
30
|
|
Vben
authored
|
31
32
33
|
export type BasicKeys = keyof BasicStore;
type LocalKeys = keyof LocalStore;
type SessionKeys = keyof SessionStore;
|
vben
authored
|
34
|
|
Vben
authored
|
35
36
|
const ls = createLocalStorage();
const ss = createSessionStorage();
|
vben
authored
|
37
|
|
Vben
authored
|
38
39
|
const localMemory = new Memory(DEFAULT_CACHE_TIME);
const sessionMemory = new Memory(DEFAULT_CACHE_TIME);
|
|
40
|
|
Vben
authored
|
41
|
function initPersistentMemory() {
|
Vben
authored
|
42
|
const localCache = ls.get(APP_LOCAL_CACHE_KEY);
|
Vben
authored
|
43
|
const sessionCache = ss.get(APP_SESSION_CACHE_KEY);
|
Vben
authored
|
44
45
|
localCache && localMemory.resetCache(localCache);
sessionCache && sessionMemory.resetCache(sessionCache);
|
|
46
|
}
|
Vben
authored
|
47
|
|
Vben
authored
|
48
49
50
|
export class Persistent {
static getLocal<T>(key: LocalKeys) {
return localMemory.get(key)?.value as Nullable<T>;
|
|
51
|
}
|
vben
authored
|
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);
}
|
vben
authored
|
57
|
|
Vben
authored
|
58
59
60
|
static removeLocal(key: LocalKeys): void {
localMemory.remove(key);
}
|
vben
authored
|
61
|
|
Vben
authored
|
62
63
|
static clearLocal(): void {
localMemory.clear();
|
vben
authored
|
64
|
}
|
|
65
|
|
Vben
authored
|
66
67
|
static getSession<T>(key: SessionKeys) {
return sessionMemory.get(key)?.value as Nullable<T>;
|
|
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);
|
|
73
74
|
}
|
Vben
authored
|
75
76
77
78
79
80
|
static removeSession(key: SessionKeys): void {
sessionMemory.remove(key);
}
static clearSession(): void {
sessionMemory.clear();
}
|
|
81
|
|
Vben
authored
|
82
83
84
85
|
static clearAll() {
sessionMemory.clear();
localMemory.clear();
}
|
|
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);
});
|
vben
authored
|
92
|
|
Vben
authored
|
93
94
|
function storageChange(e: any) {
const { key, newValue, oldValue } = e;
|
|
95
|
|
Vben
authored
|
96
97
98
99
|
if (!key) {
Persistent.clearAll();
return;
}
|
|
100
|
|
Vben
authored
|
101
102
103
|
if (!!newValue && !!oldValue) {
if (APP_LOCAL_CACHE_KEY === key) {
Persistent.clearLocal();
|
|
104
|
}
|
Vben
authored
|
105
106
|
if (APP_SESSION_CACHE_KEY === key) {
Persistent.clearSession();
|
|
107
108
|
}
}
|
Vben
authored
|
109
110
111
|
}
window.addEventListener('storage', storageChange);
|
vben
authored
|
112
|
|
Vben
authored
|
113
|
initPersistentMemory();
|