2020-11-23 23:24:13 +08:00
|
|
|
import { cacheCipher } from '/@/settings/encryptionSetting';
|
2021-02-26 20:21:13 +08:00
|
|
|
import { isNullOrUnDef } from '/@/utils/is';
|
2023-11-07 10:13:42 +08:00
|
|
|
import { Encryption, EncryptionFactory, EncryptionParams } from '@/utils/cipher';
|
2021-02-26 20:21:13 +08:00
|
|
|
|
2020-11-23 23:24:13 +08:00
|
|
|
export interface CreateStorageParams extends EncryptionParams {
|
2021-02-26 00:15:18 +08:00
|
|
|
prefixKey: string;
|
2020-11-23 23:24:13 +08:00
|
|
|
storage: Storage;
|
|
|
|
|
hasEncrypt: boolean;
|
2021-02-26 00:15:18 +08:00
|
|
|
timeout?: Nullable<number>;
|
2020-11-23 23:24:13 +08:00
|
|
|
}
|
2023-11-07 10:13:24 +08:00
|
|
|
// TODO 移除此文件夹下全部代码
|
2020-11-23 23:24:13 +08:00
|
|
|
export const createStorage = ({
|
|
|
|
|
prefixKey = '',
|
|
|
|
|
storage = sessionStorage,
|
|
|
|
|
key = cacheCipher.key,
|
|
|
|
|
iv = cacheCipher.iv,
|
2021-02-26 00:15:18 +08:00
|
|
|
timeout = null,
|
2020-11-23 23:24:13 +08:00
|
|
|
hasEncrypt = true,
|
2021-02-26 00:15:18 +08:00
|
|
|
}: Partial<CreateStorageParams> = {}) => {
|
2020-11-23 23:24:13 +08:00
|
|
|
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!');
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 10:13:42 +08:00
|
|
|
const persistEncryption: Encryption = EncryptionFactory.createAesEncryption({
|
|
|
|
|
key: cacheCipher.key,
|
|
|
|
|
iv: cacheCipher.iv,
|
|
|
|
|
});
|
2020-11-23 23:24:13 +08:00
|
|
|
/**
|
2021-11-24 23:16:53 +08:00
|
|
|
* Cache class
|
|
|
|
|
* Construction parameters can be passed into sessionStorage, localStorage,
|
2020-11-23 23:24:13 +08:00
|
|
|
* @class Cache
|
|
|
|
|
* @example
|
|
|
|
|
*/
|
|
|
|
|
const WebStorage = class WebStorage {
|
|
|
|
|
private storage: Storage;
|
|
|
|
|
private prefixKey?: string;
|
2023-11-07 10:13:42 +08:00
|
|
|
private encryption: Encryption;
|
2020-11-23 23:24:13 +08:00
|
|
|
private hasEncrypt: boolean;
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {*} storage
|
|
|
|
|
*/
|
|
|
|
|
constructor() {
|
|
|
|
|
this.storage = storage;
|
|
|
|
|
this.prefixKey = prefixKey;
|
2023-11-07 10:13:42 +08:00
|
|
|
this.encryption = persistEncryption;
|
2020-11-23 23:24:13 +08:00
|
|
|
this.hasEncrypt = hasEncrypt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getKey(key: string) {
|
|
|
|
|
return `${this.prefixKey}${key}`.toUpperCase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-24 23:16:53 +08:00
|
|
|
* Set cache
|
2020-11-23 23:24:13 +08:00
|
|
|
* @param {string} key
|
|
|
|
|
* @param {*} value
|
2021-11-24 23:16:53 +08:00
|
|
|
* @param {*} expire Expiration time in seconds
|
2020-11-23 23:24:13 +08:00
|
|
|
* @memberof Cache
|
|
|
|
|
*/
|
2021-02-26 00:15:18 +08:00
|
|
|
set(key: string, value: any, expire: number | null = timeout) {
|
2020-11-23 23:24:13 +08:00
|
|
|
const stringData = JSON.stringify({
|
|
|
|
|
value,
|
2021-02-26 20:21:13 +08:00
|
|
|
time: Date.now(),
|
|
|
|
|
expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null,
|
2020-11-23 23:24:13 +08:00
|
|
|
});
|
2023-11-07 10:13:42 +08:00
|
|
|
const stringifyValue = this.hasEncrypt ? this.encryption.encrypt(stringData) : stringData;
|
2020-11-23 23:24:13 +08:00
|
|
|
this.storage.setItem(this.getKey(key), stringifyValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-24 23:16:53 +08:00
|
|
|
* Read cache
|
2020-11-23 23:24:13 +08:00
|
|
|
* @param {string} key
|
2021-11-24 23:16:53 +08:00
|
|
|
* @param {*} def
|
2020-11-23 23:24:13 +08:00
|
|
|
* @memberof Cache
|
|
|
|
|
*/
|
|
|
|
|
get(key: string, def: any = null): any {
|
2021-02-26 20:21:13 +08:00
|
|
|
const val = this.storage.getItem(this.getKey(key));
|
|
|
|
|
if (!val) return def;
|
|
|
|
|
|
|
|
|
|
try {
|
2023-11-07 10:13:42 +08:00
|
|
|
const decVal = this.hasEncrypt ? this.encryption.decrypt(val) : val;
|
2021-02-26 20:21:13 +08:00
|
|
|
const data = JSON.parse(decVal);
|
|
|
|
|
const { value, expire } = data;
|
|
|
|
|
if (isNullOrUnDef(expire) || expire >= new Date().getTime()) {
|
|
|
|
|
return value;
|
2020-11-23 23:24:13 +08:00
|
|
|
}
|
2021-02-26 20:21:13 +08:00
|
|
|
this.remove(key);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return def;
|
2020-11-23 23:24:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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();
|
|
|
|
|
};
|