修复.cahce缓存不适配云部署的问题

This commit is contained in:
huangxd- committed 2025-11-15 10:08:30 +08:00
1 parent f12efd64c6
commit 9c2d881069
1 file changed
+12 -12
+12 -12
View File
@@ -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") {