Skip to content

enhance(response-cache/cfw-kv): simplify cache init #2238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 30, 2024
Merged

Conversation

ardatan
Copy link
Member

@ardatan ardatan commented May 20, 2024

This PR introduces the following changes;

  • Now cache parameter accepts a factory function that takes the context and returns the cache implementation
  • Now the cache implementation does not require any ctx value, but only the name of the KV namespace.

See the changes in the README for details

import { createSchema, createYoga, YogaInitialContext } from 'graphql-yoga'
import { useResponseCache } from '@envelop/response-cache'
import { createKvCache } from '@envelop/response-cache-cloudflare-kv'
import { resolvers } from './graphql-schema/resolvers.generated'
import { typeDefs } from './graphql-schema/typeDefs.generated'

export type Env = {
  GRAPHQL_RESPONSE_CACHE: KVNamespace
}

const graphqlServer = createYoga<Env & ExecutionContext>({
  schema: createSchema({ typeDefs, resolvers }),
  plugins: [
    useResponseCache({
      cache: createKvCache({
        KVName: 'GRAPHQL_RESPONSE_CACHE',
        keyPrefix: 'graphql' // optional
      }),
      session: () => null,
      includeExtensionMetadata: true,
      ttl: 1000 * 10 // 10 seconds
    })
  ]
})

export default {
  fetch: graphqlServer
}

@ardatan ardatan requested a review from EmrysMyrddin May 20, 2024 00:45
Copy link

changeset-bot bot commented May 20, 2024

🦋 Changeset detected

Latest commit: 40868c0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@envelop/response-cache Minor
@envelop/response-cache-cloudflare-kv Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented May 20, 2024

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@envelop/response-cache 6.2.0-alpha-20240528125003-40868c0a npm ↗︎ unpkg ↗︎
@envelop/response-cache-cloudflare-kv 1.0.0-alpha-20240528125003-40868c0a npm ↗︎ unpkg ↗︎

Copy link
Contributor

github-actions bot commented May 20, 2024

💻 Website Preview

The latest changes are available as preview in: https://acf91942.envelop.pages.dev

@theguild-bot
Copy link
Collaborator

theguild-bot commented May 20, 2024

✅ Benchmark Results

     ✓ no_errors
     ✓ expected_result

     checks.............................................: 100.00% ✓ 927048     ✗ 0     
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: 100.00% ✓ 217082     ✗ 0     
     ✓ { mode:envelop-cache-jit }.......................: 100.00% ✓ 364688     ✗ 0     
     ✓ { mode:envelop-just-cache }......................: 100.00% ✓ 216392     ✗ 0     
     ✓ { mode:graphql-js }..............................: 100.00% ✓ 128886     ✗ 0     
     data_received......................................: 3.5 GB  30 MB/s
     data_sent..........................................: 202 MB  1.7 MB/s
     envelop_init.......................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     envelop_total......................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     event_loop_lag.....................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     graphql_context....................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     graphql_execute....................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     graphql_parse......................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     graphql_validate...................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     http_req_blocked...................................: avg=1.93µs  min=692ns    med=1.72µs  max=11.03ms p(90)=2.35µs  p(95)=2.64µs 
     http_req_connecting................................: avg=18ns    min=0s       med=0s      max=1.04ms  p(90)=0s      p(95)=0s     
     http_req_duration..................................: avg=2.3ms   min=128.9µs  med=2.03ms  max=76.5ms  p(90)=4.17ms  p(95)=4.71ms 
       { expected_response:true }.......................: avg=2.3ms   min=128.9µs  med=2.03ms  max=76.5ms  p(90)=4.17ms  p(95)=4.71ms 
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: avg=2.48ms  min=353.83µs med=2.2ms   max=14.95ms p(90)=4.31ms  p(95)=4.69ms 
     ✓ { mode:envelop-cache-jit }.......................: avg=1.35ms  min=128.9µs  med=1.06ms  max=13.6ms  p(90)=2.16ms  p(95)=2.34ms 
     ✓ { mode:envelop-just-cache }......................: avg=2.48ms  min=352.26µs med=2.19ms  max=29.83ms p(90)=4.3ms   p(95)=4.67ms 
     ✓ { mode:graphql-js }..............................: avg=4.38ms  min=622.43µs med=3.68ms  max=76.5ms  p(90)=7.36ms  p(95)=8.05ms 
     http_req_failed....................................: 0.00%   ✓ 0          ✗ 463524
     http_req_receiving.................................: avg=31.3µs  min=12.32µs  med=27.49µs max=12.59ms p(90)=42µs    p(95)=46.83µs
     http_req_sending...................................: avg=12.29µs min=4.14µs   med=10.19µs max=9.14ms  p(90)=14.81µs p(95)=19.02µs
     http_req_tls_handshaking...........................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     http_req_waiting...................................: avg=2.26ms  min=102.58µs med=1.99ms  max=76.41ms p(90)=4.12ms  p(95)=4.66ms 
     http_reqs..........................................: 463524  3862.49141/s
     iteration_duration.................................: avg=2.58ms  min=333.98µs med=2.29ms  max=77.58ms p(90)=4.44ms  p(95)=5.02ms 
     iterations.........................................: 463524  3862.49141/s
     vus................................................: 10      min=10       max=10  
     vus_max............................................: 20      min=20       max=20  

@EmrysMyrddin
Copy link
Collaborator

Just to let you know, there is another PR (#2237) which also updates the CFW cache plugin. It updates types, so it should not be of a huge impact, I probably can do the rebase for you after merging if you want.

@ardatan
Copy link
Member Author

ardatan commented May 21, 2024

You can rebase but also keep in mind, my PR removes the need of the type compatibility. So we don't even need to deal with ReadacleStream conflict in that case.

@EmrysMyrddin
Copy link
Collaborator

We are still importing the KVNamespace type aren't we ?

You mean that the fact we don't take this as a parameter anymore avoid the typing issue in user land ?

@ardatan
Copy link
Member Author

ardatan commented May 21, 2024

@EmrysMyrddin I believe so but even if not, that is not the main goal of this PR. The main goal is to simplify the usage, move the server definition etc on top, less verbose API etc.

@EmrysMyrddin
Copy link
Collaborator

I still think it can be good to merge the other PR anyway, pinning version could avoid breaking the plugin on updates ?

@EmrysMyrddin
Copy link
Collaborator

@ardatan Is it ok to merge this ?

@ardatan
Copy link
Member Author

ardatan commented May 28, 2024

Sure you can rebase the conflicts and merge!

}
return undefined;
},
return function KVCacheFactory(ctx: TServerContext): Cache {
Copy link
Collaborator

@EmrysMyrddin EmrysMyrddin May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably even warn only once at the start of the function isn't it ?

Suggested change
return function KVCacheFactory(ctx: TServerContext): Cache {
return function KVCacheFactory(ctx: TServerContext): Cache {
if (!ctx[config.KVName]) {
// eslint-disable-next-line no-console
console.warn(
`Cloudflare KV namespace ${config.KVName} is not available in the server context, cache will be skipped.`,
);
return {
get: () => undefined,
set: () => undefined,
invalidate: () => undefined,
};
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the lifecycle of CF Workers, so I thought it might be added later on after startup. I preferred to check it lazily.

Co-authored-by: Valentin Cocaud <v.cocaud@gmail.com>
@EmrysMyrddin EmrysMyrddin merged commit 430ee7d into main May 30, 2024
18 checks passed
@EmrysMyrddin EmrysMyrddin deleted the easier-cfw-kv branch May 30, 2024 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants