vben
authored
|
1
|
import { cacheCipher } from '/@/settings/encryptionSetting';
|
Vben
authored
|
2
3
|
import type { EncryptionParams } from '/@/utils/cipher';
import { AesEncryption } from '/@/utils/cipher';
|
Vben
authored
|
4
5
|
import { isNullOrUnDef } from '/@/utils/is';
|
vben
authored
|
6
|
export interface CreateStorageParams extends EncryptionParams {
|
Vben
authored
|
7
|
prefixKey: string;
|
vben
authored
|
8
9
|
storage: Storage;
hasEncrypt: boolean;
|
Vben
authored
|
10
|
timeout?: Nullable<number>;
|
vben
authored
|
11
12
13
14
15
16
|
}
export const createStorage = ({
prefixKey = '',
storage = sessionStorage,
key = cacheCipher.key,
iv = cacheCipher.iv,
|
Vben
authored
|
17
|
timeout = null,
|
vben
authored
|
18
|
hasEncrypt = true,
|
Vben
authored
|
19
|
}: Partial<CreateStorageParams> = {}) => {
|
vben
authored
|
20
21
22
23
|
if (hasEncrypt && [key.length, iv.length].some((item) => item !== 16)) {
throw new Error('When hasEncrypt is true, the key or iv must be 16 bits!');
}
|
Vben
authored
|
24
|
const encryption = new AesEncryption({ key, iv });
|
vben
authored
|
25
26
|
/**
|
|
27
28
|
* Cache class
* Construction parameters can be passed into sessionStorage, localStorage,
|
vben
authored
|
29
30
31
32
33
34
|
* @class Cache
* @example
*/
const WebStorage = class WebStorage {
private storage: Storage;
private prefixKey?: string;
|
Vben
authored
|
35
|
private encryption: AesEncryption;
|
vben
authored
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
private hasEncrypt: boolean;
/**
*
* @param {*} storage
*/
constructor() {
this.storage = storage;
this.prefixKey = prefixKey;
this.encryption = encryption;
this.hasEncrypt = hasEncrypt;
}
private getKey(key: string) {
return `${this.prefixKey}${key}`.toUpperCase();
}
/**
|
|
53
|
* Set cache
|
vben
authored
|
54
55
|
* @param {string} key
* @param {*} value
|
|
56
|
* @param {*} expire Expiration time in seconds
|
vben
authored
|
57
58
|
* @memberof Cache
*/
|
Vben
authored
|
59
|
set(key: string, value: any, expire: number | null = timeout) {
|
vben
authored
|
60
61
|
const stringData = JSON.stringify({
value,
|
Vben
authored
|
62
63
|
time: Date.now(),
expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null,
|
vben
authored
|
64
65
66
67
68
69
70
71
|
});
const stringifyValue = this.hasEncrypt
? this.encryption.encryptByAES(stringData)
: stringData;
this.storage.setItem(this.getKey(key), stringifyValue);
}
/**
|
|
72
|
* Read cache
|
vben
authored
|
73
|
* @param {string} key
|
|
74
|
* @param {*} def
|
vben
authored
|
75
76
77
|
* @memberof Cache
*/
get(key: string, def: any = null): any {
|
Vben
authored
|
78
79
80
81
82
83
84
85
86
|
const val = this.storage.getItem(this.getKey(key));
if (!val) return def;
try {
const decVal = this.hasEncrypt ? this.encryption.decryptByAES(val) : val;
const data = JSON.parse(decVal);
const { value, expire } = data;
if (isNullOrUnDef(expire) || expire >= new Date().getTime()) {
return value;
|
vben
authored
|
87
|
}
|
Vben
authored
|
88
89
90
|
this.remove(key);
} catch (e) {
return def;
|
vben
authored
|
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
}
}
/**
* Delete cache based on key
* @param {string} key
* @memberof Cache
*/
remove(key: string) {
this.storage.removeItem(this.getKey(key));
}
/**
* Delete all caches of this instance
*/
clear(): void {
this.storage.clear();
}
};
return new WebStorage();
};
|