11import { 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" ;
33import type { OpenNextConfig } from "@opennextjs/aws/types/open-next" ;
44import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js" ;
55import { IgnorableError } from "@opennextjs/aws/utils/error.js" ;
66
77import { getCloudflareContext } from "./cloudflare-context" ;
88
9+ interface ShardedD1TagCacheOptions {
10+ numberOfShards : number ;
11+ }
912class 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 ) ;
0 commit comments