000
This commit is contained in:
parent
3429e65682
commit
f45631b205
24
source.js
24
source.js
@ -133,7 +133,7 @@ const server = http.createServer(async (req, res) => {
|
||||
} else {
|
||||
// 增加缓存命中次数
|
||||
viewsInfo.cacheHit++;
|
||||
serveFromCache(cacheData, cacheContentFile, res);
|
||||
serveFromCache(cacheData, cacheContentFile, cacheMetaFile, res);
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -155,12 +155,12 @@ const server = http.createServer(async (req, res) => {
|
||||
tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
|
||||
// 重新写入 meta 缓存
|
||||
fs.writeFileSync(cacheMetaFile, JSON.stringify(data));
|
||||
// 如果内容缓存存在, 则直接调用
|
||||
|
||||
// 如果内容缓存存在, 则直接调用
|
||||
if (fs.existsSync(cacheContentFile)) {
|
||||
serveFromCache(data, cacheContentFile, res);
|
||||
serveFromCache(data, cacheContentFile, cacheMetaFile, res);
|
||||
} else {
|
||||
fetchAndServe(data, tempCacheContentFile, cacheContentFile, res);
|
||||
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res);
|
||||
}
|
||||
} else {
|
||||
res.writeHead(502, { 'Content-Type': 'text/plain' });
|
||||
@ -234,7 +234,7 @@ const fetchApiData = (reqPath, token) => {
|
||||
};
|
||||
|
||||
// 从真实 URL 获取数据并写入缓存
|
||||
const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
|
||||
const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res) => {
|
||||
https.get(data.realUrl, { timeout: requestTimeout * 10, rejectUnauthorized: false }, (realRes) => {
|
||||
const cacheStream = fs.createWriteStream(tempCacheContentFile, { flags: 'w' });
|
||||
|
||||
@ -244,6 +244,8 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
|
||||
const contentLength = realRes.headers['content-length'];
|
||||
if (contentLength) {
|
||||
data.headers['content-length'] = contentLength;
|
||||
// 更新 data 到缓存 cacheMetaFile
|
||||
fs.writeFileSync(cacheMetaFile, JSON.stringify(data));
|
||||
} else {
|
||||
console.warn('Warning: content-length is undefined for the response from:', data.realUrl);
|
||||
}
|
||||
@ -285,17 +287,27 @@ const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
|
||||
};
|
||||
|
||||
// 从缓存中读取数据并返回
|
||||
const serveFromCache = (cacheData, cacheContentFile, res) => {
|
||||
const serveFromCache = (cacheData, cacheContentFile, cacheMetaFile, res) => {
|
||||
// 增加缓存调用次数
|
||||
viewsInfo.cacheCall++;
|
||||
const readStream = fs.createReadStream(cacheContentFile);
|
||||
let isVideo = cacheData.path && typeof cacheData.path === 'string' && cacheData.path.includes('.mp4');
|
||||
|
||||
|
||||
// 查询 cacheData.headers['content-length'] 是否存在
|
||||
if (!cacheData.headers['content-length'] || cacheData.headers['content-length'] === 0) {
|
||||
// 读取文件大小并更新 cacheData.headers['content-length']
|
||||
const contentLength = fs.statSync(cacheContentFile).size;
|
||||
if (contentLength) {
|
||||
cacheData.headers['content-length'] = contentLength;
|
||||
// 更新 cacheData 到缓存 cacheMetaFile
|
||||
fs.writeFileSync(cacheMetaFile, JSON.stringify(cacheData));
|
||||
} else {
|
||||
console.warn('Warning: content-length is undefined for cached content file:', cacheContentFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
readStream.on('open', () => {
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user