Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,32 +294,27 @@ 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[]) {
try {
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;
}
}

async putToRegionalCache(doId: DOId, tags: string[], hasBeenRevalidated: boolean) {
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}` },
})
Expand All @@ -332,8 +327,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);
}
Expand Down