persistent.ts 3.2 KB

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