From 9c2d881069e48349c7116734818a35ea681a74cd Mon Sep 17 00:00:00 2001 From: huangxd- <649924757@qq.com> Date: Sat, 15 Nov 2025 10:08:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D.cahce=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E4=B8=8D=E9=80=82=E9=85=8D=E4=BA=91=E9=83=A8=E7=BD=B2=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- danmu_api/utils/cache-util.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/danmu_api/utils/cache-util.js b/danmu_api/utils/cache-util.js index b069fa4..4ec51b4 100644 --- a/danmu_api/utils/cache-util.js +++ b/danmu_api/utils/cache-util.js @@ -309,9 +309,19 @@ export function cleanupExpiredIPs(currentTime) { } } +// 获取当前文件目录的兼容方式 +function getDirname() { + if (typeof __dirname !== 'undefined') { + // CommonJS 环境 (Vercel 编译后) + return __dirname; + } + // ES Module 环境 + return path.dirname(fileURLToPath(import.meta.url)); +} + // 从本地缓存目录读取缓存数据 export function readCacheFromFile(key) { - const cacheFilePath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '.cache', `${key}`); + const cacheFilePath = path.join(getDirname(), '..', '..', '.cache', `${key}`); if (fs.existsSync(cacheFilePath)) { const fileContent = fs.readFileSync(cacheFilePath, 'utf8'); return JSON.parse(fileContent); @@ -321,7 +331,7 @@ export function readCacheFromFile(key) { // 将缓存数据写入本地缓存文件 export function writeCacheToFile(key, value) { - const cacheFilePath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '.cache', `${key}`); + const cacheFilePath = path.join(getDirname(), '..', '..', '.cache', `${key}`); fs.writeFileSync(cacheFilePath, JSON.stringify(value), 'utf8'); } @@ -398,16 +408,6 @@ export async function updateLocalCaches() { } } -// 获取当前文件目录的兼容方式 -function getDirname() { - if (typeof __dirname !== 'undefined') { - // CommonJS 环境 (Vercel 编译后) - return __dirname; - } - // ES Module 环境 - return path.dirname(fileURLToPath(import.meta.url)); -} - // 判断是否有效的本地缓存目录 export function judgeLocalCacheValid(urlPath) { if (!globals.localCacheValid && urlPath !== "/favicon.ico" && urlPath !== "/robots.txt") {