storageCache.ts 2.8 KB

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