Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/many-days-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

perf: optimize SQL queries
23 changes: 10 additions & 13 deletions packages/cloudflare/src/api/durable-objects/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,19 +281,16 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
checkSyncTable(msg: QueueMessage) {
try {
if (this.disableSQLite) return false;
const numNewer = this.sql
.exec<{
numNewer: number;
}>(
"SELECT COUNT(*) as numNewer FROM sync WHERE id = ? AND lastSuccess > ? LIMIT 1",
`${msg.MessageBody.host}${msg.MessageBody.url}`,
Math.round(msg.MessageBody.lastModified / 1000)
)
.one().numNewer;

return numNewer > 0;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e: unknown) {
return (
this.sql
.exec(
"SELECT 1 FROM sync WHERE id = ? AND lastSuccess > ? LIMIT 1",
`${msg.MessageBody.host}${msg.MessageBody.url}`,
Math.round(msg.MessageBody.lastModified / 1000)
)
.toArray().length > 0
);
} catch {
return false;
}
}
Expand Down
19 changes: 9 additions & 10 deletions packages/cloudflare/src/api/durable-objects/sharded-tag-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ export class DOShardedTagCache extends DurableObject<CloudflareEnv> {
}

async hasBeenRevalidated(tags: string[], lastModified?: number): Promise<boolean> {
const result = this.sql
.exec<{
cnt: number;
}>(
`SELECT COUNT(*) as cnt FROM revalidations WHERE tag IN (${tags.map(() => "?").join(", ")}) AND revalidatedAt > ?`,
...tags,
lastModified ?? Date.now()
)
.one();
return result.cnt > 0;
return (
this.sql
.exec(
`SELECT 1 FROM revalidations WHERE tag IN (${tags.map(() => "?").join(", ")}) AND revalidatedAt > ? LIMIT 1`,
...tags,
lastModified ?? Date.now()
)
.toArray().length > 0
);
}

async writeTags(tags: string[], lastModified: number): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ export class D1NextModeTagCache implements NextModeTagCache {
try {
const result = await db
.prepare(
`SELECT COUNT(*) as cnt FROM revalidations WHERE tag IN (${tags.map(() => "?").join(", ")}) AND revalidatedAt > ? LIMIT 1`
`SELECT 1 FROM revalidations WHERE tag IN (${tags.map(() => "?").join(", ")}) AND revalidatedAt > ? LIMIT 1`
)
.bind(...tags.map((tag) => this.getCacheKey(tag)), lastModified ?? Date.now())
.first<{ cnt: number }>();
if (!result) throw new RecoverableError(`D1 select failed for ${tags}`);
.raw();

return result.cnt > 0;
return result.length > 0;
} catch (e) {
error(e);
// By default we don't want to crash here, so we return false
Expand Down