feat: 添加服务器端缓存功能并优化详情页数据获取

refactor(details/[id].vue): 重构详情页数据获取逻辑,加入缓存机制
feat(plugins/cache.server.js): 新增node-cache插件用于服务器端缓存
chore: 更新package.json依赖,添加node-cache和clone
This commit is contained in:
DESKTOP-RQ919RC\Pc
2025-07-08 11:10:20 +08:00
parent 68acefa360
commit 335d6cb1fb
48 changed files with 1758 additions and 466 deletions

View File

@@ -654,7 +654,8 @@
</div>
<template v-else>
<div class="respond-pop-title">
<span class="respond-pop-amount">{{ ripostecount.user }}</span>人回应
<span class="respond-pop-amount">{{ ripostecount.user }}</span
>人回应
<img class="respond-title-icon" @click="closePopList()" src="@/assets/img/cross-grey.png" />
</div>
<div class="respond-list">
@@ -1334,8 +1335,8 @@ const replaceState = (uni) => {
// 替换当前URL但不刷新页面
window.history.pushState({}, "", `${window.location.origin}/details/${uni}`);
console.log("uni",uni,"location",location);
console.log("uni", uni, "location", location);
if (location.pathname.indexOf(uni) == -1) XSTAT.trackNewPage();
};
@@ -1356,39 +1357,53 @@ const report = (token) => {
provide("reportAlertShow", reportAlertShow);
provide("clearAllData", clearAllData);
provide("getDetails", getDetails);
// seo的
if (process.server) {
try {
await detailsHttp({ uniqid }).then((res) => {
if (res.code != 200) {
ElMessage.error(res.message);
return;
}
let data = res.data;
token = data["token"];
info.value = data["info"];
seo.value = data.seo;
iscollection.value = data.iscollection;
isdisplay.value = data.isdisplay;
islike.value = data.islike;
ismyself.value = data.ismyself;
qrcode.value = data["share"]["qrcode"];
if (relatedlist.value.length == 0) getRelatedlistHttp();
else CalculateSelectedList();
detailsLoading.value = false;
getCommentListHttp();
});
const { $cache } = useNuxtApp();
// seo的
if (process.server && process.env.NODE_ENV != "development") {
try {
const detailKey = `details_${uniqid}`;
const cachedData = $cache.get(detailKey);
// console.log(cachedData ? "缓存数据已存在" : "缓存数据不存在");
if (cachedData) {
const infoData = cachedData["info"] || {};
info.value = infoData;
seo.value = cachedData.seo;
iscollection.value = cachedData.iscollection;
isdisplay.value = cachedData.isdisplay;
islike.value = cachedData.islike;
ismyself.value = cachedData.ismyself;
qrcode.value = cachedData["share"]["qrcode"];
} else {
await detailsHttp({ uniqid }).then((res) => {
if (res.code == 200) {
let data = res.data;
token = data["token"];
const infoData = data["info"] || {};
info.value = infoData;
seo.value = data.seo;
iscollection.value = data.iscollection;
isdisplay.value = data.isdisplay;
islike.value = data.islike;
ismyself.value = data.ismyself;
qrcode.value = data["share"]["qrcode"];
// if (relatedlist.value.length == 0) getRelatedlistHttp();
// else CalculateSelectedList();
// detailsLoading.value = false;
// getCommentListHttp();
$cache.set(detailKey, data, 3600);
}
});
}
await relatedlistHttp({ token, page: 1 }).then((res) => {
if (res.code != 200) return;
let data = res.data;
relatedlist.value = data.data;
relatedcount.value = data.count;
CalculateSelectedList();
// CalculateSelectedList();
});
} catch (error) {}
}
@@ -2177,7 +2192,6 @@ const selectEditEmoji = (key) => {
};
const postEditComment = () => {
if (isNeedLogin.value) {
goLogin();
return;