Skip to content

Commit

Permalink
优化random接口的读取策略
Browse files Browse the repository at this point in the history
  • Loading branch information
MarSeventh committed Dec 20, 2024
1 parent 83bba59 commit 2748200
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 93 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Add Features:
- 管理端批量操作支持按照用户选择的顺序进行([#issue124](https://github.com/MarSeventh/CloudFlare-ImgBed/issues/124)
- `random`接口优化,减少KV操作次数,增加`content`参数,支持返回指定类型的文件
- 接入CloudFlare Cache API,提升 list 相关接口访问速度
- 正常读取返回图片的CDN缓存时间从1年调整为7天,防止缓存清除不成功的情况下图片长时间内仍可以访问的问题

## 2024.12.14

Expand Down
67 changes: 0 additions & 67 deletions functions/api/randomFileList.js

This file was deleted.

8 changes: 4 additions & 4 deletions functions/file/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ export async function onRequest(context) { // Contents of context object
if (fileType) {
headers.set('Content-Type', fileType);
}
// 根据Referer设置CDN缓存策略,如果是从/或/dashboard等访问,则仅允许浏览器缓存;否则设置为public,缓存时间为1年
// 根据Referer设置CDN缓存策略,如果是从/或/dashboard等访问,则仅允许浏览器缓存;否则设置为public,缓存时间为7天
if (Referer && Referer.includes(url.origin)) {
headers.set('Cache-Control', 'private, max-age=86400');
} else {
headers.set('Cache-Control', 'public, max-age=31536000');
headers.set('Cache-Control', 'public, max-age=604800');
}

// 返回图片
Expand Down Expand Up @@ -136,11 +136,11 @@ export async function onRequest(context) { // Contents of context object
if (fileType) {
headers.set('Content-Type', fileType);
}
// 根据Referer设置CDN缓存策略,如果是从/或/dashboard等访问,则仅允许浏览器缓存;否则设置为public,缓存时间为1年
// 根据Referer设置CDN缓存策略,如果是从/或/dashboard等访问,则仅允许浏览器缓存;否则设置为public,缓存时间为7天
if (Referer && Referer.includes(url.origin)) {
headers.set('Cache-Control', 'private, max-age=86400');
} else {
headers.set('Cache-Control', 'public, max-age=31536000');
headers.set('Cache-Control', 'public, max-age=604800');
}

const newRes = new Response(response.body, {
Expand Down
20 changes: 0 additions & 20 deletions functions/login.js

This file was deleted.

47 changes: 45 additions & 2 deletions functions/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export async function onRequest(context) {
}

// 调用randomFileList接口,读取KV数据库中的所有记录
let allRecords = [];
allRecords = JSON.parse(await fetch(requestUrl.origin + '/api/randomFileList').then(res => res.text()));
let allRecords = await getRandomFileList(env, requestUrl);

// 筛选出符合fileType要求的记录
allRecords = allRecords.filter(item => { return fileType.some(type => item.FileType.includes(type)) });
Expand Down Expand Up @@ -73,3 +72,47 @@ export async function onRequest(context) {
}
}
}

async function getRandomFileList(env, url) {
// 检查缓存中是否有记录,有则直接返回
const cache = caches.default;
const cacheRes = await cache.match(`${url.origin}/api/randomFileList`);
if (cacheRes) {
return JSON.parse(await cacheRes.text());
}

let allRecords = [];
let cursor = null;

do {
const records = await env.img_url.list({
limit: 1000,
cursor,
});
// 除去records中key以manage@开头的记录
records.keys = records.keys.filter(item => !item.name.startsWith("manage@"));
// 保留metadata中fileType为image或video的记录
records.keys = records.keys.filter(item => item.metadata?.FileType?.includes("image") || item.metadata?.FileType?.includes("video"));
allRecords.push(...records.keys);
cursor = records.cursor;
} while (cursor);

// 仅保留记录的name和metadata中的FileType字段
allRecords = allRecords.map(item => {
return {
name: item.name,
FileType: item.metadata?.FileType
}
});

// 缓存结果,缓存时间为24小时
await cache.put(`${url.origin}/api/randomFileList`, new Response(JSON.stringify(allRecords), {
headers: {
"Content-Type": "application/json",
}
}), {
expirationTtl: 24 * 60 * 60
});

return allRecords;
}

0 comments on commit 2748200

Please sign in to comment.