@@ -35,14 +35,16 @@ type Options = {
35
35
* Whether the regional cache entry should be updated in the background or not when it experiences
36
36
* a cache hit.
37
37
*
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 .
39
39
*/
40
40
shouldLazilyUpdateOnCacheHit ?: boolean ;
41
41
42
42
/**
43
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.
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.
46
48
*
47
49
* @default `true` if the auto cache purging is enabled, `false` otherwise.
48
50
*/
@@ -56,7 +58,16 @@ interface PutToCacheInput {
56
58
}
57
59
58
60
/**
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).
60
71
*/
61
72
class RegionalCache implements IncrementalCache {
62
73
public name : string ;
@@ -71,7 +82,8 @@ class RegionalCache implements IncrementalCache {
71
82
throw new Error ( "The KV incremental cache does not need a regional cache." ) ;
72
83
}
73
84
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;
75
87
}
76
88
77
89
get #bypassTagCacheOnCacheHit( ) : boolean {
@@ -103,7 +115,8 @@ class RegionalCache implements IncrementalCache {
103
115
if ( cachedResponse ) {
104
116
debugCache ( "Get - cached response" ) ;
105
117
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.
107
120
if ( this . opts . shouldLazilyUpdateOnCacheHit ) {
108
121
getCloudflareContext ( ) . ctx . waitUntil (
109
122
this . store . get ( key , cacheType ) . then ( async ( rawEntry ) => {
0 commit comments