Ben Lin
2024-06-18 ebbd788fbb2c0b45d4473798efc57eec8ba74a25
src/utils/cache/storageCache.ts
@@ -1,7 +1,6 @@
import { cacheCipher } from '/@/settings/encryptionSetting';
import type { EncryptionParams } from '/@/utils/cipher';
import { AesEncryption } from '/@/utils/cipher';
import { isNullOrUnDef } from '/@/utils/is';
import { cacheCipher } from '@/settings/encryptionSetting';
import { isNil } from '@/utils/is';
import { Encryption, EncryptionFactory, EncryptionParams } from '@/utils/cipher';
export interface CreateStorageParams extends EncryptionParams {
  prefixKey: string;
@@ -9,6 +8,7 @@
  hasEncrypt: boolean;
  timeout?: Nullable<number>;
}
// TODO 移除此文件夹下全部代码
export const createStorage = ({
  prefixKey = '',
  storage = sessionStorage,
@@ -21,8 +21,10 @@
    throw new Error('When hasEncrypt is true, the key or iv must be 16 bits!');
  }
  const encryption = new AesEncryption({ key, iv });
  const persistEncryption: Encryption = EncryptionFactory.createAesEncryption({
    key: cacheCipher.key,
    iv: cacheCipher.iv,
  });
  /**
   * Cache class
   * Construction parameters can be passed into sessionStorage, localStorage,
@@ -32,7 +34,7 @@
  const WebStorage = class WebStorage {
    private storage: Storage;
    private prefixKey?: string;
    private encryption: AesEncryption;
    private encryption: Encryption;
    private hasEncrypt: boolean;
    /**
     *
@@ -41,7 +43,7 @@
    constructor() {
      this.storage = storage;
      this.prefixKey = prefixKey;
      this.encryption = encryption;
      this.encryption = persistEncryption;
      this.hasEncrypt = hasEncrypt;
    }
@@ -60,11 +62,9 @@
      const stringData = JSON.stringify({
        value,
        time: Date.now(),
        expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null,
        expire: !isNil(expire) ? new Date().getTime() + expire * 1000 : null,
      });
      const stringifyValue = this.hasEncrypt
        ? this.encryption.encryptByAES(stringData)
        : stringData;
      const stringifyValue = this.hasEncrypt ? this.encryption.encrypt(stringData) : stringData;
      this.storage.setItem(this.getKey(key), stringifyValue);
    }
@@ -79,10 +79,10 @@
      if (!val) return def;
      try {
        const decVal = this.hasEncrypt ? this.encryption.decryptByAES(val) : val;
        const decVal = this.hasEncrypt ? this.encryption.decrypt(val) : val;
        const data = JSON.parse(decVal);
        const { value, expire } = data;
        if (isNullOrUnDef(expire) || expire >= new Date().getTime()) {
        if (isNil(expire) || expire >= new Date().getTime()) {
          return value;
        }
        this.remove(key);