hookehuyr

fix: 修复内存泄漏并优化缓存管理

修复版本检查定时器未清理导致的内存泄漏问题
优化axios缓存机制,减少内存占用和过期缓存清理频率
......@@ -2,7 +2,7 @@
* @Author: hookehuyr hookehuyr@gmail.com
* @Date: 2022-05-26 23:52:36
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-06-10 13:47:08
* @LastEditTime: 2025-10-01 15:41:19
* @FilePath: /data-table/src/App.vue
* @Description:
-->
......@@ -12,7 +12,7 @@
<script setup>
import { mainStore, useTitle } from "@/utils/generatePackage";
import { computed, watchEffect, onMounted } from "vue";
import { computed, watchEffect, onMounted, onUnmounted } from "vue";
import { useRoute, useRouter } from "vue-router";
// import { Toast } from "vant";
// 会根据配置判断是否显示调试控件
......@@ -242,8 +242,9 @@ onMounted(async () => {
}
// TAG:检查是否更新
let upDater = null;
if (import.meta.env.PROD) {
const upDater = new Updater({
upDater = new Updater({
time: 30000
})
upDater.on('no-update', () => {
......@@ -259,6 +260,13 @@ onMounted(async () => {
});
})
}
// 组件卸载时清理定时器
onUnmounted(() => {
if (upDater) {
upDater.destroy();
}
});
});
</script>
......
......@@ -2,7 +2,7 @@
* @Author: hookehuyr hookehuyr@gmail.com
* @Date: 2022-05-28 10:17:40
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-11-25 15:56:19
* @LastEditTime: 2025-09-30 22:25:22
* @FilePath: /data-table/src/utils/axios.js
* @Description:
*/
......@@ -27,7 +27,7 @@ const onlyDataCache = {
const { method, url, params, data } = config;
var key = [method, url].join("&");
if (fullPath) {
key = [method, url, stringify(params), stringify(data)].join("&");
key = [method, url, JSON.stringify(params), JSON.stringify(data)].join("&");
}
return key;
},
......@@ -70,9 +70,9 @@ const keepDataCache = {
// 缓存列表
list: [],
// 最大缓存数
MAX_NUM: 100,
MAX_NUM: 50, // 从100减少到50,减少内存占用
// 最大缓存时间
EXPIRED_TIME: 60000,
EXPIRED_TIME: 30000, // 从60秒减少到30秒,更频繁地清理过期缓存
// 根据请求的信息(请求方式,url,请求get/post数据),产生map的key
getRequestKey(config) {
const { method, url, params, data } = config;
......@@ -91,10 +91,14 @@ const keepDataCache = {
if (index > -1) {
// 保存请求结果
this.list[index].data = data;
this.list[index].time = Date.now(); // 更新时间戳
} else {
// 添加到缓存列表中
this.list.push({ time: Date.now(), key, data });
}
// 主动清理过期缓存
this.cleanExpiredCache();
},
// 查找缓存结果
find(config) {
......@@ -115,11 +119,21 @@ const keepDataCache = {
}
}
// 判断是否超出了最大缓存数量
if (this.list.length === this.MAX_NUM) {
if (this.list.length >= this.MAX_NUM) {
// 移除最旧的缓存项
this.list.shift();
}
// 返回undefined,让请求拦截不执行config.adapter
return undefined;
},
// 清理过期缓存的方法
cleanExpiredCache() {
const now = Date.now();
this.list = this.list.filter(item => now - item.time <= this.EXPIRED_TIME);
},
// 清空所有缓存
clear() {
this.list = [];
}
};
......
/*
* @Date: 2024-02-06 11:38:13
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-02-06 13:04:25
* @FilePath: /xysBooking/src/utils/versionUpdater.js
* @LastEditTime: 2025-09-30 22:33:40
* @FilePath: /data-table/src/utils/versionUpdater.js
* @Description:
*/
/* eslint-disable */
......@@ -61,10 +61,20 @@ export class Updater {
timing(time = 10000) {
//轮询
setInterval(async () => {
this.intervalId = setInterval(async () => {
const newHtml = await this.getHtml();
this.newScript = this.parserScript(newHtml);
this.compare(this.oldScript, this.newScript);
}, time);
}
/**
* 清理定时器
*/
destroy() {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
}
}
......