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