refactor(route/bilibili): optimize username and face caching logic

This commit is contained in:
DIYgod
2025-08-16 16:23:23 +08:00
parent 396bc0c2f2
commit fb6d4f3689
3 changed files with 16 additions and 8 deletions
+2 -2
View File
@@ -147,11 +147,11 @@ const getUsernameAndFaceFromUID = async (uid) => {
if (nameResponse.data.name) {
name = nameResponse.data.name;
face = nameResponse.data.face;
cache.set(nameKey, nameResponse.data.name);
cache.set(faceKey, nameResponse.data.face);
} else {
logger.error(`Error when visiting /x/space/wbi/acc/info: ${JSON.stringify(nameResponse)}`);
}
cache.set(nameKey, name);
cache.set(faceKey, face);
}
return [name, face];
};
+10 -5
View File
@@ -271,11 +271,16 @@ async function handler(ctx) {
}
const items = (body as BilibiliWebDynamicResponse)?.data?.items;
const usernameAndFace = await cacheIn.getUsernameAndFaceFromUID(uid);
const author = usernameAndFace[0] || items[0]?.modules?.module_author?.name;
const face = usernameAndFace[1] || items[0]?.modules?.module_author?.face;
cache.set(`bili-username-from-uid-${uid}`, author);
cache.set(`bili-userface-from-uid-${uid}`, face);
let author = items[0]?.modules?.module_author?.name;
let face = items[0]?.modules?.module_author?.face;
if (!face || !author) {
const usernameAndFace = await cacheIn.getUsernameAndFaceFromUID(uid);
author = usernameAndFace[0] || items[0]?.modules?.module_author?.name;
face = usernameAndFace[1] || items[0]?.modules?.module_author?.face;
} else {
cache.set(`bili-username-from-uid-${uid}`, author);
cache.set(`bili-userface-from-uid-${uid}`, face);
}
const rssItems = await Promise.all(
items
+4 -1
View File
@@ -39,7 +39,6 @@ async function handler(ctx: Context) {
const dmImgList = utils.getDmImgList();
const dmImgInter = utils.getDmImgInter();
const renderData = await cache.getRenderData(uid);
const [name, face] = await cache.getUsernameAndFaceFromUID(uid);
const params = utils.addWbiVerifyInfo(
utils.addRenderData(utils.addDmVerifyInfoWithInter(`mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&platform=web&web_location=1550101&order_avoided=true`, dmImgList, dmImgInter), renderData),
@@ -57,6 +56,10 @@ async function handler(ctx: Context) {
throw new Error(`Got error code ${data.code} while fetching: ${data.message}`);
}
const usernameAndFace = await cache.getUsernameAndFaceFromUID(uid);
const name = usernameAndFace[0] || data.data.list.vlist[0]?.author;
const face = usernameAndFace[1];
return {
title: `${name} 的 bilibili 空间`,
link: `https://space.bilibili.com/${uid}`,