devCache.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import devOptions from "./devOptions"
  2. /**
  3. * dev工具缓存管理
  4. */
  5. export default {
  6. /**
  7. * 存储的键开始名称
  8. */
  9. cacheKey: "devTools_v3_",
  10. options: null,
  11. /**
  12. * 临时缓存对象
  13. */
  14. tempCache: {
  15. errorReport: [],
  16. logReport: [],
  17. console: [],
  18. request: [],
  19. uniBus: [],
  20. },
  21. /**
  22. * 临时数据存放
  23. */
  24. tempData: {},
  25. /**
  26. * 向缓存内写入数据
  27. */
  28. set(key, value) {
  29. try {
  30. if (['errorReport', 'logReport', 'console', 'request', 'uniBus'].indexOf(key) != -1) {
  31. let setting = this.getLongListSetting(key)
  32. if (!setting.status) return;
  33. if (!setting.cache.status) {
  34. // !不使用缓存
  35. this.tempCache[key] = value;
  36. return;
  37. }
  38. }
  39. key = `${this.cacheKey}${key}`
  40. // #ifdef APP-NVUE
  41. let pages = getCurrentPages()
  42. if (pages[pages.length - 1].route == "devTools/page/index") {
  43. // devtools 页面直接走设置缓存
  44. return uni.setStorageSync(key, value);
  45. }
  46. // #endif
  47. this.tempData[key] = value;
  48. } catch (error) {
  49. console.log("devCache.set error", error);
  50. }
  51. },
  52. /**
  53. * 同步读取缓存数据
  54. */
  55. get(key) {
  56. try {
  57. if (['errorReport', 'logReport', 'console', 'request', 'uniBus'].indexOf(key) != -1) {
  58. let setting = this.getLongListSetting(key)
  59. if (!setting.status) return [];
  60. if (!setting.cache.status) {
  61. // !不使用缓存
  62. return this.tempCache[key];
  63. }
  64. }
  65. key = `${this.cacheKey}${key}`
  66. // #ifdef APP-NVUE
  67. let pages = getCurrentPages()
  68. if (pages[pages.length - 1].route == "devTools/page/index") {
  69. // devtools 页面直接走设置缓存
  70. return uni.getStorageSync(key);
  71. }
  72. // #endif
  73. if (this.tempData.hasOwnProperty(key)) {
  74. return this.tempData[key];
  75. } else {
  76. let value = uni.getStorageSync(key);
  77. this.tempData[key] = value;
  78. return value;
  79. }
  80. } catch (error) {
  81. console.log("devCache.get error", error);
  82. return "";
  83. }
  84. },
  85. getLongListSetting(key) {
  86. let optionsKey = {
  87. errorReport: 'error',
  88. logReport: 'logs',
  89. console: 'console',
  90. request: 'network',
  91. uniBus: 'uniBus',
  92. }
  93. if (this.options) return this.options[optionsKey[key]];
  94. this.options = devOptions.getOptions()
  95. return this.options[optionsKey[key]];
  96. },
  97. /**
  98. * 同步本地缓存
  99. */
  100. syncLocalCache() {
  101. let that = this;
  102. setTimeout(() => {
  103. try {
  104. let waitSetKeys = Object.keys(that.tempData);
  105. for (let i = 0; i < waitSetKeys.length; i++) {
  106. const key = waitSetKeys[i];
  107. uni.setStorage({
  108. key,
  109. data: that.tempData[key],
  110. success() {
  111. // console.log("set " + key + " success,length=" + that.tempData[key].length);
  112. delete that.tempData[key];
  113. }
  114. });
  115. }
  116. } catch (error) {
  117. console.log("devCache error: ", error);
  118. }
  119. setTimeout(() => {
  120. that.syncLocalCache();
  121. }, 500);
  122. }, Math.round(Math.random() * 3 * 1000) + 2000);
  123. },
  124. }