@@ -38,7 +38,7 @@ export function createKvCache<
38
38
} & {
39
39
waitUntil ( promise : Promise < unknown > ) : void ;
40
40
} ,
41
- > ( config : KvCacheConfig < TKVNamespaceName > ) : ( ctx : TServerContext ) => Cache {
41
+ > ( config : KvCacheConfig < TKVNamespaceName > ) {
42
42
if ( config . cacheReadTTL && config . cacheReadTTL < 60000 ) {
43
43
// eslint-disable-next-line no-console
44
44
console . warn (
@@ -47,17 +47,21 @@ export function createKvCache<
47
47
}
48
48
const computedTtlInSeconds = Math . max ( Math . floor ( ( config . cacheReadTTL ?? 60000 ) / 1000 ) , 60 ) ; // KV TTL must be at least 60 seconds
49
49
50
- return function KVCacheFactory ( ctx : TServerContext ) {
50
+ return function KVCacheFactory ( ctx : TServerContext ) : Cache {
51
51
return {
52
- async get ( id : string ) {
53
- const kvResponse = await ctx [ config . KVName ] . get ( buildOperationKey ( id , config . keyPrefix ) , {
54
- type : 'text' ,
52
+ get ( id : string ) {
53
+ if ( ! ctx [ config . KVName ] ) {
54
+ // eslint-disable-next-line no-console
55
+ console . warn (
56
+ `Cloudflare KV namespace ${ config . KVName } is not available in the server context, skipping cache read.` ,
57
+ ) ;
58
+ return ;
59
+ }
60
+ const operationKey = buildOperationKey ( id , config . keyPrefix ) ;
61
+ return ctx [ config . KVName ] . get ( operationKey , {
62
+ type : 'json' ,
55
63
cacheTtl : computedTtlInSeconds ,
56
64
} ) ;
57
- if ( kvResponse ) {
58
- return JSON . parse ( kvResponse ) as ExecutionResult ;
59
- }
60
- return undefined ;
61
65
} ,
62
66
63
67
set (
@@ -69,12 +73,42 @@ export function createKvCache<
69
73
entities : Iterable < CacheEntityRecord > ,
70
74
/** how long the operation should be cached (in milliseconds) */
71
75
ttl : number ,
72
- ) {
76
+ ) : void | Promise < void > {
77
+ if ( ! ctx [ config . KVName ] ) {
78
+ // eslint-disable-next-line no-console
79
+ console . warn (
80
+ `Cloudflare KV namespace ${ config . KVName } is not available in the server context, skipping cache write.` ,
81
+ ) ;
82
+ return ;
83
+ }
84
+ const setPromise = set ( id , data , entities , ttl , ctx [ config . KVName ] , config . keyPrefix ) ;
85
+ if ( ! ctx . waitUntil ) {
86
+ // eslint-disable-next-line no-console
87
+ console . warn (
88
+ 'The server context does not have a waitUntil method. This means that the cache write will not be non-blocking.' ,
89
+ ) ;
90
+ return setPromise ;
91
+ }
73
92
// Do not block execution of the worker while caching the result
74
- ctx . waitUntil ( set ( id , data , entities , ttl , ctx [ config . KVName ] , config . keyPrefix ) ) ;
93
+ ctx . waitUntil ( setPromise ) ;
75
94
} ,
76
95
77
- invalidate ( entities : Iterable < CacheEntityRecord > ) {
96
+ invalidate ( entities : Iterable < CacheEntityRecord > ) : void | Promise < void > {
97
+ if ( ! ctx [ config . KVName ] ) {
98
+ // eslint-disable-next-line no-console
99
+ console . warn (
100
+ `Cloudflare KV namespace ${ config . KVName } is not available in the server context, skipping cache invalidate.` ,
101
+ ) ;
102
+ return ;
103
+ }
104
+ const invalidatePromise = invalidate ( entities , ctx [ config . KVName ] , config . keyPrefix ) ;
105
+ if ( ! ctx . waitUntil ) {
106
+ // eslint-disable-next-line no-console
107
+ console . warn (
108
+ 'The server context does not have a waitUntil method. This means that the cache invalidation will not be non-blocking.' ,
109
+ ) ;
110
+ return invalidatePromise ;
111
+ }
78
112
// Do not block execution of the worker while invalidating the cache
79
113
ctx . waitUntil ( invalidate ( entities , ctx [ config . KVName ] , config . keyPrefix ) ) ;
80
114
} ,
0 commit comments