Skip to content

Commit ce5c7b4

Browse files
dario-piotrowiczconico974james-elicx
authored
Add option for regional cache to skip tagCache on cache hits (#850)
--------- Co-authored-by: conico974 <[email protected]> Co-authored-by: James Anderson <[email protected]>
1 parent ebe7249 commit ce5c7b4

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

.changeset/lovely-gifts-cough.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
"@opennextjs/cloudflare": minor
3+
---
4+
5+
Add option for regional cache to skip tagCache on cache hits
6+
7+
When the tag regional cache finds a value in the incremental cache, checking such value in the tagCache can be skipped, this helps reducing response times at the tradeoff that the user needs to either use the automatic cache purging or manually purge the cache when appropriate. For this the `bypassTagCacheOnCacheHit` option is being added to the `RegionalCache` class.
8+
9+
Example:
10+
11+
```js
12+
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
13+
import d1NextTagCache from "@opennextjs/cloudflare/overrides/tag-cache/d1-next-tag-cache";
14+
import memoryQueue from "@opennextjs/cloudflare/overrides/queue/memory-queue";
15+
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
16+
import { withRegionalCache } from "@opennextjs/cloudflare/overrides/incremental-cache/regional-cache";
17+
18+
export default defineCloudflareConfig({
19+
incrementalCache: withRegionalCache(r2IncrementalCache, {
20+
mode: "long-lived",
21+
bypassTagCacheOnCacheHit: true,
22+
}),
23+
tagCache: d1NextTagCache,
24+
queue: memoryQueue,
25+
});
26+
```

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ type Options = {
3838
* @default `false` for the `short-lived` mode, and `true` for the `long-lived` mode.
3939
*/
4040
shouldLazilyUpdateOnCacheHit?: boolean;
41+
42+
/**
43+
* 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.
46+
*
47+
* @default `true` if the auto cache purging is enabled, `false` otherwise.
48+
*/
49+
bypassTagCacheOnCacheHit?: boolean;
4150
};
4251

4352
interface PutToCacheInput {
@@ -65,6 +74,17 @@ class RegionalCache implements IncrementalCache {
6574
this.opts.shouldLazilyUpdateOnCacheHit ??= this.opts.mode === "long-lived";
6675
}
6776

77+
get #bypassTagCacheOnCacheHit(): boolean {
78+
if (this.opts.bypassTagCacheOnCacheHit !== undefined) {
79+
// If the bypassTagCacheOnCacheHit option is set we return that one
80+
return this.opts.bypassTagCacheOnCacheHit;
81+
}
82+
83+
// Otherwise we default to whether the automatic cache purging is enabled or not
84+
const hasAutomaticCachePurging = !!getCloudflareContext().env.NEXT_CACHE_DO_PURGE;
85+
return hasAutomaticCachePurging;
86+
}
87+
6888
async get<CacheType extends CacheEntryType = "cache">(
6989
key: string,
7090
cacheType?: CacheType
@@ -91,7 +111,12 @@ class RegionalCache implements IncrementalCache {
91111
);
92112
}
93113

94-
return cachedResponse.json();
114+
const responseJson: Record<string, unknown> = await cachedResponse.json();
115+
116+
return {
117+
...responseJson,
118+
shouldBypassTagCache: this.#bypassTagCacheOnCacheHit,
119+
};
95120
}
96121

97122
const rawEntry = await this.store.get(key, cacheType);

0 commit comments

Comments
 (0)