Skip to content

Commit a6283af

Browse files
authored
perf: skip lazy update on cache hit when cache purge is enabled (#882)
1 parent 9a746d2 commit a6283af

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

.changeset/pink-dodos-decide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
perf: skip lazy update on cache hit when cache purge is enabled

packages/cloudflare/src/api/overrides/incremental-cache/regional-cache.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ type Options = {
3535
* Whether the regional cache entry should be updated in the background or not when it experiences
3636
* a cache hit.
3737
*
38-
* @default `false` for the `short-lived` mode, and `true` for the `long-lived` mode.
38+
* @default `true` in `long-lived` mode when cache purge is not used, `false` otherwise.
3939
*/
4040
shouldLazilyUpdateOnCacheHit?: boolean;
4141

4242
/**
4343
* Whether on cache hits the tagCache should be skipped or not. Skipping the tagCache allows requests to be
44-
* handled faster, the downside of this is that you need to make sure that the cache gets correctly purged
45-
* either by enabling the auto cache purging feature or doing that manually.
44+
* handled faster,
45+
*
46+
* Note: When this is enabled, make sure that the cache gets purged
47+
* either by enabling the auto cache purging feature or manually.
4648
*
4749
* @default `true` if the auto cache purging is enabled, `false` otherwise.
4850
*/
@@ -56,7 +58,16 @@ interface PutToCacheInput {
5658
}
5759

5860
/**
59-
* Wrapper adding a regional cache on an `IncrementalCache` implementation
61+
* Wrapper adding a regional cache on an `IncrementalCache` implementation.
62+
*
63+
* Using a the `RegionalCache` does not directly improves the performance much.
64+
* However it allows bypassing the tag cache (see `bypassTagCacheOnCacheHit`) on hits.
65+
* That's where bigger perf gain happens.
66+
*
67+
* We recommend using cache purge.
68+
* When cache purge is not enabled, there is a possibility that the Cache API (local to a Data Center)
69+
* is out of sync with the cache store (i.e. R2). That's why when cache purge is not enabled the Cache
70+
* API is refreshed from the cache store on cache hits (for the long-lived mode).
6071
*/
6172
class RegionalCache implements IncrementalCache {
6273
public name: string;
@@ -71,7 +82,8 @@ class RegionalCache implements IncrementalCache {
7182
throw new Error("The KV incremental cache does not need a regional cache.");
7283
}
7384
this.name = this.store.name;
74-
this.opts.shouldLazilyUpdateOnCacheHit ??= this.opts.mode === "long-lived";
85+
this.opts.shouldLazilyUpdateOnCacheHit ??=
86+
this.opts.mode === "long-lived" && !this.#hasAutomaticCachePurging;
7587
}
7688

7789
get #bypassTagCacheOnCacheHit(): boolean {
@@ -103,7 +115,8 @@ class RegionalCache implements IncrementalCache {
103115
if (cachedResponse) {
104116
debugCache("Get - cached response");
105117

106-
// Re-fetch from the store and update the regional cache in the background
118+
// Re-fetch from the store and update the regional cache in the background.
119+
// Note: this is only useful when the Cache API is not purged automatically.
107120
if (this.opts.shouldLazilyUpdateOnCacheHit) {
108121
getCloudflareContext().ctx.waitUntil(
109122
this.store.get(key, cacheType).then(async (rawEntry) => {

0 commit comments

Comments
 (0)