diff --git a/packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.spec.ts b/packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.spec.ts index bb7f4f12..a148393c 100644 --- a/packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.spec.ts +++ b/packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.spec.ts @@ -310,16 +310,18 @@ describe("DOShardedTagCache", () => { it("should return the cache key without the random part", async () => { const cache = shardedDOTagCache(); const doId1 = new DOId({ baseShardId: "shard-0", numberOfReplicas: 1, shardType: "hard" }); - const reqKey = await cache.getCacheKey(doId1, ["_N_T_/tag1"]); - expect(reqKey.url).toBe("http://local.cache/shard/tag-hard;shard-0?tags=_N_T_%2Ftag1"); + expect(cache.getCacheUrlKey(doId1, ["_N_T_/tag1"])).toBe( + "http://local.cache/shard/tag-hard;shard-0?tags=_N_T_%2Ftag1" + ); const doId2 = new DOId({ baseShardId: "shard-1", numberOfReplicas: 1, shardType: "hard", }); - const reqKey2 = await cache.getCacheKey(doId2, ["tag1"]); - expect(reqKey2.url).toBe("http://local.cache/shard/tag-hard;shard-1?tags=tag1"); + expect(cache.getCacheUrlKey(doId2, ["tag1"])).toBe( + "http://local.cache/shard/tag-hard;shard-1?tags=tag1" + ); }); }); diff --git a/packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.ts b/packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.ts index d0cd2e9c..da20e5db 100644 --- a/packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.ts +++ b/packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.ts @@ -294,10 +294,8 @@ class ShardedDOTagCache implements NextModeTagCache { return this.localCache; } - async getCacheKey(doId: DOId, tags: string[]) { - return new Request( - new URL(`shard/${doId.shardId}?tags=${encodeURIComponent(tags.join(";"))}`, "http://local.cache") - ); + getCacheUrlKey(doId: DOId, tags: string[]): string { + return `http://local.cache/shard/${doId.shardId}?tags=${encodeURIComponent(tags.join(";"))}`; } async getFromRegionalCache(doId: DOId, tags: string[]) { @@ -305,11 +303,9 @@ class ShardedDOTagCache implements NextModeTagCache { if (!this.opts.regionalCache) return; const cache = await this.getCacheInstance(); if (!cache) return; - const key = await this.getCacheKey(doId, tags); - return cache.match(key); + return cache.match(this.getCacheUrlKey(doId, tags)); } catch (e) { error("Error while fetching from regional cache", e); - return; } } @@ -317,11 +313,17 @@ class ShardedDOTagCache implements NextModeTagCache { if (!this.opts.regionalCache) return; const cache = await this.getCacheInstance(); if (!cache) return; - const key = await this.getCacheKey(doId, tags); await cache.put( - key, + this.getCacheUrlKey(doId, tags), new Response(`${hasBeenRevalidated}`, { - headers: { "cache-control": `max-age=${this.opts.regionalCacheTtlSec ?? 5}` }, + headers: { + "cache-control": `max-age=${this.opts.regionalCacheTtlSec ?? 5}`, + ...(tags.length > 0 + ? { + "cache-tag": tags.join(","), + } + : {}), + }, }) ); } @@ -332,8 +334,7 @@ class ShardedDOTagCache implements NextModeTagCache { if (!this.opts.regionalCache) return; const cache = await this.getCacheInstance(); if (!cache) return; - const key = await this.getCacheKey(doId, tags); - await cache.delete(key); + await cache.delete(this.getCacheUrlKey(doId, tags)); } catch (e) { debugCache("Error while deleting from regional cache", e); }