persistent.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { createStorage } from '/@/utils/storage';
  2. import { isIeFn } from '/@/utils/browser';
  3. import { BASE_LOCAL_CACHE_KEY, BASE_SESSION_CACHE_KEY } from '/@/enums/cacheEnum';
  4. const ls = createStorage(localStorage);
  5. const ss = createStorage();
  6. interface CacheStore {
  7. local: Record<string, any>;
  8. session: Record<string, any>;
  9. }
  10. /**
  11. * @description: Persistent cache
  12. */
  13. const cacheStore: CacheStore = {
  14. // localstorage cache
  15. local: {},
  16. // sessionstorage cache
  17. session: {},
  18. };
  19. function initCache() {
  20. cacheStore.local = ls.get(BASE_LOCAL_CACHE_KEY) || {};
  21. cacheStore.session = ss.get(BASE_SESSION_CACHE_KEY) || {};
  22. }
  23. initCache();
  24. export function setLocal(key: string, value: any, immediate = false) {
  25. cacheStore.local[BASE_LOCAL_CACHE_KEY] = cacheStore.local[BASE_LOCAL_CACHE_KEY] || {};
  26. cacheStore.local[BASE_LOCAL_CACHE_KEY][key] = value;
  27. if (immediate) {
  28. ls.set(BASE_LOCAL_CACHE_KEY, cacheStore.local);
  29. }
  30. }
  31. export function getLocal<T>(key: string): T | null {
  32. try {
  33. return cacheStore.local[BASE_LOCAL_CACHE_KEY][key];
  34. } catch (error) {
  35. return null;
  36. }
  37. }
  38. export function removeLocal(key: string) {
  39. if (cacheStore.local[BASE_LOCAL_CACHE_KEY]) {
  40. Reflect.deleteProperty(cacheStore.local[BASE_LOCAL_CACHE_KEY], key);
  41. }
  42. }
  43. export function clearLocal() {
  44. cacheStore.local = {};
  45. }
  46. export function setSession(key: string, value: any, immediate = false) {
  47. cacheStore.session[BASE_SESSION_CACHE_KEY] = cacheStore.session[BASE_SESSION_CACHE_KEY] || {};
  48. cacheStore.session[BASE_SESSION_CACHE_KEY][key] = value;
  49. if (immediate) {
  50. const cache = cacheStore.session;
  51. ss.set(BASE_SESSION_CACHE_KEY, cache);
  52. }
  53. }
  54. export function removeSession(key: string) {
  55. if (cacheStore.session[BASE_SESSION_CACHE_KEY]) {
  56. Reflect.deleteProperty(cacheStore.session[BASE_SESSION_CACHE_KEY], key);
  57. }
  58. }
  59. export function getSession<T>(key: string): T | null {
  60. try {
  61. return cacheStore.session[BASE_SESSION_CACHE_KEY][key];
  62. } catch (error) {
  63. return null;
  64. }
  65. }
  66. export function clearSession() {
  67. cacheStore.session = {};
  68. }
  69. export function clearAll() {
  70. clearLocal();
  71. clearSession();
  72. }
  73. (() => {
  74. // /** Write to local before closing window */
  75. window.addEventListener('beforeunload', () => {
  76. const localCache = cacheStore.local;
  77. const sessionCache = cacheStore.session;
  78. ls.set(BASE_LOCAL_CACHE_KEY, localCache);
  79. ss.set(BASE_SESSION_CACHE_KEY, sessionCache);
  80. });
  81. function storageChange(e: any) {
  82. const { key, newValue, oldValue } = e;
  83. if (!key) {
  84. clearAll();
  85. return;
  86. }
  87. if (!!newValue && !!oldValue) {
  88. if (BASE_LOCAL_CACHE_KEY === key) {
  89. clearLocal();
  90. }
  91. if (BASE_SESSION_CACHE_KEY === key) {
  92. clearSession();
  93. }
  94. }
  95. }
  96. if (isIeFn() && (document as any).attachEvent) {
  97. (document as any).attachEvent('onstorage', storageChange);
  98. } else {
  99. window.addEventListener('storage', storageChange);
  100. }
  101. })();