Skip to content

Commit e3e549a

Browse files
committed
use generateShardId
1 parent 464e299 commit e3e549a

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

examples/e2e/app-router/open-next.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import shardedTagCache from "@opennextjs/cloudflare/do-sharded-tag-cache";
66

77
export default defineCloudflareConfig({
88
incrementalCache: kvIncrementalCache,
9-
tagCache: shardedTagCache,
9+
tagCache: shardedTagCache({ numberOfShards: 4 }),
1010
queue: doQueue,
1111
});

packages/cloudflare/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@
7373
"dependencies": {
7474
"@ast-grep/napi": "^0.36.1",
7575
"@dotenvx/dotenvx": "catalog:",
76-
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@773",
76+
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@778",
7777
"enquirer": "^2.4.1",
7878
"glob": "catalog:",
7979
"yaml": "^2.7.0"
8080
},
8181
"peerDependencies": {
8282
"wrangler": "catalog:"
8383
}
84-
}
84+
}

packages/cloudflare/src/api/do-sharded-tag-cache.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import { debug } from "@opennextjs/aws/adapters/logger.js";
2-
import { generateMessageGroupId } from "@opennextjs/aws/core/routing/queue.js";
2+
import { generateShardId } from "@opennextjs/aws/core/routing/queue.js";
33
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next";
44
import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js";
55
import { IgnorableError } from "@opennextjs/aws/utils/error.js";
66

77
import { getCloudflareContext } from "./cloudflare-context";
88

9+
interface ShardedD1TagCacheOptions {
10+
numberOfShards: number;
11+
}
912
class ShardedD1TagCache implements NextModeTagCache {
1013
mode = "nextMode" as const;
1114
public readonly name = "sharded-d1-tag-cache";
1215

16+
constructor(private opts: ShardedD1TagCacheOptions = { numberOfShards: 4 }) { }
17+
1318
private getDurableObjectStub(shardId: string) {
1419
const durableObject = getCloudflareContext().env.NEXT_CACHE_D1_SHARDED;
1520
if (!durableObject) throw new IgnorableError("No durable object binding for cache revalidation");
@@ -20,7 +25,7 @@ class ShardedD1TagCache implements NextModeTagCache {
2025

2126
private generateShards(tags: string[]) {
2227
// For each tag, we generate a message group id
23-
const messageGroupIds = tags.map((tag) => ({ shardId: generateMessageGroupId(tag), tag }));
28+
const messageGroupIds = tags.map((tag) => ({ shardId: generateShardId(tag, this.opts.numberOfShards, "shard"), tag }));
2429
// We group the tags by shard
2530
const shards = new Map<string, string[]>();
2631
for (const { shardId, tag } of messageGroupIds) {
@@ -54,23 +59,20 @@ class ShardedD1TagCache implements NextModeTagCache {
5459
const { isDisabled } = this.getConfig();
5560
if (isDisabled) return false;
5661
const shards = this.generateShards(tags);
57-
let hasBeenRevalidated = false;
58-
console.log("Checking revalidation for tags", tags, shards);
5962
// We then create a new durable object for each shard
60-
await Promise.all(
63+
const shardsResult = await Promise.all(
6164
Array.from(shards.entries()).map(async ([shardId, shardedTags]) => {
6265
const stub = this.getDurableObjectStub(shardId);
63-
hasBeenRevalidated = hasBeenRevalidated || (await stub.hasBeenRevalidated(shardedTags, lastModified));
66+
return stub.hasBeenRevalidated(shardedTags, lastModified)
6467
})
6568
);
66-
return hasBeenRevalidated;
69+
return shardsResult.some((result) => result);
6770
}
6871

6972
async writeTags(tags: string[]): Promise<void> {
7073
const { isDisabled } = this.getConfig();
7174
if (isDisabled) return;
7275
const shards = this.generateShards(tags);
73-
console.log("Writing tags", tags, shards);
7476
// We then create a new durable object for each shard
7577
await Promise.all(
7678
Array.from(shards.entries()).map(async ([shardId, shardedTags]) => {
@@ -81,4 +83,4 @@ class ShardedD1TagCache implements NextModeTagCache {
8183
}
8284
}
8385

84-
export default new ShardedD1TagCache();
86+
export default (opts?: ShardedD1TagCacheOptions) => new ShardedD1TagCache(opts);

packages/cloudflare/src/api/durable-objects/sharded-tag-cache.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class DOShardedTagCache extends DurableObject<CloudflareEnv> {
2121
lastModified ?? Date.now()
2222
)
2323
.one();
24+
console.log("Checking revalidation for tags", tags, result);
2425
return result.cnt > 0;
2526
}
2627

@@ -32,5 +33,6 @@ export class DOShardedTagCache extends DurableObject<CloudflareEnv> {
3233
Date.now()
3334
);
3435
});
36+
console.log("Writing tags", tags);
3537
}
3638
}

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)