persistent.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { createStorage } from '/@/utils/storage/index';
  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?: any;
  8. session?: 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. const localCache = cacheStore.local;
  29. ls.set(BASE_LOCAL_CACHE_KEY, localCache);
  30. }
  31. }
  32. export function getLocal<T>(key: string): T | null {
  33. try {
  34. return cacheStore.local[BASE_LOCAL_CACHE_KEY][key];
  35. } catch (error) {
  36. return null;
  37. }
  38. }
  39. export function removeLocal(key: string) {
  40. if (cacheStore.local[BASE_LOCAL_CACHE_KEY]) {
  41. Reflect.deleteProperty(cacheStore.local[BASE_LOCAL_CACHE_KEY], key);
  42. }
  43. }
  44. export function clearLocal() {
  45. cacheStore.local = {};
  46. }
  47. export function setSession(key: string, value: any, immediate = false) {
  48. cacheStore.session[BASE_SESSION_CACHE_KEY] = cacheStore.session[BASE_SESSION_CACHE_KEY] || {};
  49. cacheStore.session[BASE_SESSION_CACHE_KEY][key] = value;
  50. if (immediate) {
  51. const cache = cacheStore.session;
  52. ls.set(BASE_SESSION_CACHE_KEY, cache);
  53. }
  54. }
  55. export function removeSession(key: string) {
  56. if (cacheStore.session[BASE_SESSION_CACHE_KEY]) {
  57. Reflect.deleteProperty(cacheStore.session[BASE_SESSION_CACHE_KEY], key);
  58. }
  59. }
  60. export function getSession<T>(key: string): T | null {
  61. try {
  62. return cacheStore.session[BASE_SESSION_CACHE_KEY][key];
  63. } catch (error) {
  64. return null;
  65. }
  66. }
  67. export function clearSession() {
  68. cacheStore.session = {};
  69. }
  70. export function clearAll() {
  71. clearLocal();
  72. clearSession();
  73. }
  74. (() => {
  75. // /** Write to local before closing window */
  76. window.addEventListener('beforeunload', () => {
  77. const localCache = cacheStore.local;
  78. const sessionCache = cacheStore.session;
  79. // const ss = createStorage();
  80. ls.set(BASE_LOCAL_CACHE_KEY, localCache);
  81. ss.set(BASE_SESSION_CACHE_KEY, sessionCache);
  82. });
  83. function storageChange(e: any) {
  84. const { key, newValue, oldValue } = e;
  85. if (!key) {
  86. clearAll();
  87. return;
  88. }
  89. if (!!newValue && !!oldValue) {
  90. if (BASE_LOCAL_CACHE_KEY === key) {
  91. clearLocal();
  92. }
  93. if (BASE_SESSION_CACHE_KEY === key) {
  94. clearSession();
  95. }
  96. }
  97. }
  98. if (isIeFn() && (document as any).attachEvent) {
  99. (document as any).attachEvent('onstorage', storageChange);
  100. } else {
  101. window.addEventListener('storage', storageChange);
  102. }
  103. })();