Feat: Cache search results for an hour so other ?i= are free and fast

A search's result URLs are kept in SurrealDB (results table) for 1 hour,
fetched in the same transaction as the rate limit. Other ?i= of the query
then skip the Brave/Bing search and don't count toward the daily limit.
The record also tracks which results worked (pos) and how far it got
(next), so an already-known ?i= downloads only its own image and a new one
carries on from where the last left off. free.* searches Openverse and
Wikimedia together so its whole list can be cached. Empty results aren't
cached, since they can mean Brave and Bing failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-12 14:07:51 +00:00
co-authored by Claude Opus 5.5
parent bd8ef51591
commit 223150650c
4 changed files with 66 additions and 43 deletions
+4 -5
View File
@@ -13,9 +13,8 @@ export function parseFreeSource(searchParams) {
return src === "auto" || Object.hasOwn(SOURCES, src) ? src : null;
}
// Yields each source's image URLs in turn; a source is only searched once the previous one runs out
export async function* freeImageUrls(query, src, env) {
for (const name of src === "auto" ? Object.keys(SOURCES) : [src]) {
yield* (await SOURCES[name](query, env, FREE_UA)) || [];
}
// Every source's image URLs, one source after another. All are searched at once, since the whole list gets cached.
export async function freeImageUrls(query, src, env) {
const names = src === "auto" ? Object.keys(SOURCES) : [src];
return (await Promise.all(names.map(n => SOURCES[n](query, env, FREE_UA)))).flatMap(urls => urls || []);
}