Skip to content

Commit 5bd1363

Browse files
authored
Update _worker.js
在删除文件时自动删除缓存。
1 parent 8ca5ef3 commit 5bd1363

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

_worker.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -441,18 +441,10 @@ async function handleRootRequest(request, USERNAME, PASSWORD, enableAuth) {
441441
}
442442

443443
async function handleAdminRequest(DATABASE, request, USERNAME, PASSWORD) {
444-
const cache = caches.default;
445-
const cacheKey = new Request(request.url);
446444
if (!authenticate(request, USERNAME, PASSWORD)) {
447445
return new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="Admin"' } });
448446
}
449-
const cachedResponse = await cache.match(cacheKey);
450-
if (cachedResponse) {
451-
return cachedResponse;
452-
}
453-
const response = await generateAdminPage(DATABASE);
454-
await cache.put(cacheKey, response.clone());
455-
return response;
447+
return await generateAdminPage(DATABASE);
456448
}
457449

458450
function isValidCredentials(authHeader, USERNAME, PASSWORD) {
@@ -849,6 +841,11 @@ async function handleDeleteImagesRequest(request, DATABASE) {
849841
}
850842
const placeholders = keysToDelete.map(() => '?').join(',');
851843
await DATABASE.prepare(`DELETE FROM media WHERE url IN (${placeholders})`).bind(...keysToDelete).run();
844+
const cache = caches.default;
845+
for (const url of keysToDelete) {
846+
const cacheKey = new Request(url);
847+
await cache.delete(cacheKey);
848+
}
852849
return new Response(JSON.stringify({ message: '删除成功' }), { status: 200 });
853850
} catch (error) {
854851
console.error('删除图片时出错:', error);

0 commit comments

Comments
 (0)