Skip to content

Commit df0f1dd

Browse files
add bypassTagCacheOnCacheHit option
1 parent 4e4fe42 commit df0f1dd

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-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: 11 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` is the auto cache purging is enabled, `false` otherwise.
48+
*/
49+
bypassTagCacheOnCacheHit?: boolean;
4150
};
4251

4352
interface PutToCacheInput {
@@ -63,6 +72,7 @@ class RegionalCache implements IncrementalCache {
6372
}
6473
this.name = this.store.name;
6574
this.opts.shouldLazilyUpdateOnCacheHit ??= this.opts.mode === "long-lived";
75+
this.opts.bypassTagCacheOnCacheHit ??= getCloudflareContext().env.NEXT_CACHE_DO_PURGE ? true : false;
6676
}
6777

6878
async get<CacheType extends CacheEntryType = "cache">(
@@ -103,7 +113,7 @@ class RegionalCache implements IncrementalCache {
103113
this.putToCache({ key, cacheType, entry: { value, lastModified } })
104114
);
105115

106-
return { value, lastModified, shouldBypassTagCache: true };
116+
return { value, lastModified, shouldBypassTagCache: this.opts.bypassTagCacheOnCacheHit };
107117
} catch (e) {
108118
error("Failed to get from regional cache", e);
109119
return null;

0 commit comments

Comments
 (0)