Fix Bug: 随机图缓存问题修复

This commit is contained in:
MarSeventh
2025-03-07 18:54:01 +08:00
4 changed files with 70 additions and 52 deletions

View File

@@ -70,62 +70,68 @@ export async function onRequest(context) {
try {
// 读取图片信息
const img = await env.img_url.getWithMetadata(fileId);
// 读取图片信息
const img = await env.img_url.getWithMetadata(fileId);
// 如果是R2渠道的图片删除R2中对应的图片
if (img.metadata?.Channel === 'CloudflareR2') {
await env.img_r2.delete(fileId);
}
// 如果是R2渠道的图片删除R2中对应的图片
if (img.metadata?.Channel === 'CloudflareR2') {
await env.img_r2.delete(fileId);
}
// S3 渠道的图片删除S3中对应的图片
if (img.metadata?.Channel === "S3") {
const s3Client = new S3Client({
region: img.metadata?.S3Region || "auto", // 默认使用 auto 区域
endpoint: img.metadata?.S3Endpoint,
credentials: {
accessKeyId: img.metadata?.S3AccessKeyId,
secretAccessKey: img.metadata?.S3SecretAccessKey
},
});
// S3 渠道的图片删除S3中对应的图片
if (img.metadata?.Channel === "S3") {
const s3Client = new S3Client({
region: img.metadata?.S3Region || "auto", // 默认使用 auto 区域
endpoint: img.metadata?.S3Endpoint,
credentials: {
accessKeyId: img.metadata?.S3AccessKeyId,
secretAccessKey: img.metadata?.S3SecretAccessKey
},
});
const bucketName = img.metadata?.S3BucketName;
const key = img.metadata?.S3FileKey;
const bucketName = img.metadata?.S3BucketName;
const key = img.metadata?.S3FileKey;
try {
const command = new DeleteObjectCommand({
Bucket: bucketName,
Key: key,
});
try {
const command = new DeleteObjectCommand({
Bucket: bucketName,
Key: key,
});
await s3Client.send(command);
} catch (error) {
return new Response(`Error: S3 Delete Failed - ${error.message}`, { status: 500 });
}
}
await s3Client.send(command);
} catch (error) {
return new Response(`Error: S3 Delete Failed - ${error.message}`, { status: 500 });
}
}
// 删除KV中的图片信息
await env.img_url.delete(fileId);
const info = JSON.stringify(fileId);
// 删除KV中的图片信息
await env.img_url.delete(fileId);
const info = JSON.stringify(fileId);
// 清除CDN缓存
await purgeCFCache(env, cdnUrl);
// 清除CDN缓存
await purgeCFCache(env, cdnUrl);
// 清除api/randomFileList API缓存
try {
const cache = caches.default;
// 通过写入一个max-age=0的response来清除缓存
const nullResponse = new Response(null, {
headers: { 'Cache-Control': 'max-age=0' },
});
const keys = await cache.keys();
for (let key of keys) {
if (key.url.includes('/api/randomFileList')) {
await cache.put(`${url.origin}/api/randomFileList`, nullResponse);
}
}
} catch (error) {
console.error('Failed to clear cache:', error);
}
// 清除api/randomFileList API缓存
try {
const cache = caches.default;
// 通过写入一个max-age=0的response来清除缓存
const nullResponse = new Response(null, {
headers: { 'Cache-Control': 'max-age=0' },
});
await cache.put(`${url.origin}/api/randomFileList`, nullResponse);
} catch (error) {
console.error('Failed to clear cache:', error);
}
return new Response(info);
return new Response(info);
} catch (e) {
return new Response('Error: Delete Image Failed', { status: 400 });
return new Response('Error: Delete Image Failed', { status: 400 });
}
}

View File

@@ -143,7 +143,13 @@ export async function onRequest(context) {
const nullResponse = new Response(null, {
headers: { 'Cache-Control': 'max-age=0' },
});
await cache.put(`${url.origin}/api/randomFileList`, nullResponse);
const keys = await cache.keys();
for (let key of keys) {
if (key.url.includes('/api/randomFileList')) {
await cache.put(`${url.origin}/api/randomFileList`, nullResponse);
}
}
} catch (error) {
console.error('Failed to clear cache:', error);
}

View File

@@ -89,7 +89,7 @@ export async function onRequest(context) {
async function getRandomFileList(env, url, dir) {
// 检查缓存中是否有记录,有则直接返回
const cache = caches.default;
const cacheRes = await cache.match(`${url.origin}/api/randomFileList`);
const cacheRes = await cache.match(`${url.origin}/api/randomFileList?dir=${dir}`);
if (cacheRes) {
return JSON.parse(await cacheRes.text());
}
@@ -120,7 +120,7 @@ async function getRandomFileList(env, url, dir) {
});
// 缓存结果缓存时间为24小时
await cache.put(`${url.origin}/api/randomFileList`, new Response(JSON.stringify(allRecords), {
await cache.put(`${url.origin}/api/randomFileList?dir=${dir}`, new Response(JSON.stringify(allRecords), {
headers: {
"Content-Type": "application/json",
}

View File

@@ -673,7 +673,13 @@ async function purgeCDNCache(env, cdnUrl, url) {
const nullResponse = new Response(null, {
headers: { 'Cache-Control': 'max-age=0' },
});
await cache.put(`${url.origin}/api/randomFileList`, nullResponse);
const keys = await cache.keys();
for (let key of keys) {
if (key.url.includes('/api/randomFileList')) {
await cache.put(`${url.origin}/api/randomFileList`, nullResponse);
}
}
} catch (error) {
console.error('Failed to clear cache:', error);
}