storageCache.ts 2.8 KB

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