Skip to content

Commit 6700c5a

Browse files
authored
refactor: make cache key a string (#590)
1 parent 3a51a28 commit 6700c5a

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,16 +310,18 @@ describe("DOShardedTagCache", () => {
310310
it("should return the cache key without the random part", async () => {
311311
const cache = shardedDOTagCache();
312312
const doId1 = new DOId({ baseShardId: "shard-0", numberOfReplicas: 1, shardType: "hard" });
313-
const reqKey = await cache.getCacheKey(doId1, ["_N_T_/tag1"]);
314-
expect(reqKey.url).toBe("http://local.cache/shard/tag-hard;shard-0?tags=_N_T_%2Ftag1");
313+
expect(cache.getCacheUrlKey(doId1, ["_N_T_/tag1"])).toBe(
314+
"http://local.cache/shard/tag-hard;shard-0?tags=_N_T_%2Ftag1"
315+
);
315316

316317
const doId2 = new DOId({
317318
baseShardId: "shard-1",
318319
numberOfReplicas: 1,
319320
shardType: "hard",
320321
});
321-
const reqKey2 = await cache.getCacheKey(doId2, ["tag1"]);
322-
expect(reqKey2.url).toBe("http://local.cache/shard/tag-hard;shard-1?tags=tag1");
322+
expect(cache.getCacheUrlKey(doId2, ["tag1"])).toBe(
323+
"http://local.cache/shard/tag-hard;shard-1?tags=tag1"
324+
);
323325
});
324326
});
325327

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,34 +294,36 @@ class ShardedDOTagCache implements NextModeTagCache {
294294
return this.localCache;
295295
}
296296

297-
async getCacheKey(doId: DOId, tags: string[]) {
298-
return new Request(
299-
new URL(`shard/${doId.shardId}?tags=${encodeURIComponent(tags.join(";"))}`, "http://local.cache")
300-
);
297+
getCacheUrlKey(doId: DOId, tags: string[]): string {
298+
return `http://local.cache/shard/${doId.shardId}?tags=${encodeURIComponent(tags.join(";"))}`;
301299
}
302300

303301
async getFromRegionalCache(doId: DOId, tags: string[]) {
304302
try {
305303
if (!this.opts.regionalCache) return;
306304
const cache = await this.getCacheInstance();
307305
if (!cache) return;
308-
const key = await this.getCacheKey(doId, tags);
309-
return cache.match(key);
306+
return cache.match(this.getCacheUrlKey(doId, tags));
310307
} catch (e) {
311308
error("Error while fetching from regional cache", e);
312-
return;
313309
}
314310
}
315311

316312
async putToRegionalCache(doId: DOId, tags: string[], hasBeenRevalidated: boolean) {
317313
if (!this.opts.regionalCache) return;
318314
const cache = await this.getCacheInstance();
319315
if (!cache) return;
320-
const key = await this.getCacheKey(doId, tags);
321316
await cache.put(
322-
key,
317+
this.getCacheUrlKey(doId, tags),
323318
new Response(`${hasBeenRevalidated}`, {
324-
headers: { "cache-control": `max-age=${this.opts.regionalCacheTtlSec ?? 5}` },
319+
headers: {
320+
"cache-control": `max-age=${this.opts.regionalCacheTtlSec ?? 5}`,
321+
...(tags.length > 0
322+
? {
323+
"cache-tag": tags.join(","),
324+
}
325+
: {}),
326+
},
325327
})
326328
);
327329
}
@@ -332,8 +334,7 @@ class ShardedDOTagCache implements NextModeTagCache {
332334
if (!this.opts.regionalCache) return;
333335
const cache = await this.getCacheInstance();
334336
if (!cache) return;
335-
const key = await this.getCacheKey(doId, tags);
336-
await cache.delete(key);
337+
await cache.delete(this.getCacheUrlKey(doId, tags));
337338
} catch (e) {
338339
debugCache("Error while deleting from regional cache", e);
339340
}

0 commit comments

Comments
 (0)