feat: 优化性能

This commit is contained in:
539943419 committed 2025-01-13 01:49:55 +08:00
1 parent 40379326b5
commit 7086e336cb
12 files changed
+438 -67

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
import { defineStore } from "pinia";
import type { postContent, postItem } from "~/types/post";
const useDefaultStore = defineStore("postStore", () => {
const postCache = ref<Record<string, postContent>>({});
const lastUpdateTime = ref<Record<string, number>>({});
const postListCache = ref<Record<string, postItem[]>>({});
const postListLastUpdateTime = ref<number>(-1);
function getPostCache(key: string) {
if (lastUpdateTime.value[key] - Date.now() > 1000 * 60) {
delete postCache.value[key];
delete lastUpdateTime.value[key];
}
if (postCache.value[key]) {
return postCache.value[key];
}
return undefined;
}
function setPostCache(key: string, value: postContent) {
postCache.value[key] = value;
lastUpdateTime.value[key] = Date.now();
}
function deletePostCache(key: string) {
delete postCache.value[key];
delete lastUpdateTime.value[key];
}
return {
lastUpdateTime, postCache, postListCache, postListLastUpdateTime,
getPostCache, setPostCache, deletePostCache
};
});
export default useDefaultStore;