@@ -38,7 +38,7 @@ export function createKvCache<
3838 } & {
3939 waitUntil ( promise : Promise < unknown > ) : void ;
4040 } ,
41- > ( config : KvCacheConfig < TKVNamespaceName > ) : ( ctx : TServerContext ) => Cache {
41+ > ( config : KvCacheConfig < TKVNamespaceName > ) {
4242 if ( config . cacheReadTTL && config . cacheReadTTL < 60000 ) {
4343 // eslint-disable-next-line no-console
4444 console . warn (
@@ -47,17 +47,21 @@ export function createKvCache<
4747 }
4848 const computedTtlInSeconds = Math . max ( Math . floor ( ( config . cacheReadTTL ?? 60000 ) / 1000 ) , 60 ) ; // KV TTL must be at least 60 seconds
4949
50- return function KVCacheFactory ( ctx : TServerContext ) {
50+ return function KVCacheFactory ( ctx : TServerContext ) : Cache {
5151 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' ,
5563 cacheTtl : computedTtlInSeconds ,
5664 } ) ;
57- if ( kvResponse ) {
58- return JSON . parse ( kvResponse ) as ExecutionResult ;
59- }
60- return undefined ;
6165 } ,
6266
6367 set (
@@ -69,12 +73,42 @@ export function createKvCache<
6973 entities : Iterable < CacheEntityRecord > ,
7074 /** how long the operation should be cached (in milliseconds) */
7175 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+ }
7392 // 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 ) ;
7594 } ,
7695
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+ }
78112 // Do not block execution of the worker while invalidating the cache
79113 ctx . waitUntil ( invalidate ( entities , ctx [ config . KVName ] , config . keyPrefix ) ) ;
80114 } ,
0 commit comments