apollo-server icon indicating copy to clipboard operation
apollo-server copied to clipboard

Add Dynamic Prefix To Cache Key

Open jorgechacons opened this issue 2 years ago • 6 comments

Adding hook to pass a function with access to request context to return a dynamic prefix for each key. This feature allows the developers to pass identifiers to the keys, so it makes it searchable for redis-cli or memcached that use key-based searches.

jorgechacons avatar May 12 '22 22:05 jorgechacons

@jorgechacon04: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

apollo-cla avatar May 12 '22 22:05 apollo-cla

Deploy Preview for apollo-server-docs canceled.

Built without sensitive environment variables

Name Link
Latest commit c29bec46d2f95a1085534c05757ef1bda002addb
Latest deploy log https://app.netlify.com/sites/apollo-server-docs/deploys/628e68091576fb0008d5b8e4

netlify[bot] avatar May 12 '22 22:05 netlify[bot]

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c29bec46d2f95a1085534c05757ef1bda002addb:

Sandbox Source
Apollo Server Typescript Configuration
Apollo Server Configuration

codesandbox-ci[bot] avatar May 12 '22 22:05 codesandbox-ci[bot]

Would it make more sense to let you override cacheKeyString instead? (Note that this would also subsume the functionality of extraCacheKeyData which could be deprecated.)

glasser avatar Jun 03 '22 01:06 glasser

@glasser That sounds good too, however at least for our case extraCacheKeyData is not used, because that will be hashed so it is not searchable for redis

I added a new hook, to be able to share the requestContext to extract data like operationName, etc

jorgechacons avatar Jun 03 '22 19:06 jorgechacons

I think I might not have been clear here. I feel like the concept of "dynamic prefix" is too specific. Both "dynamic prefix" and "extra cache key data" are ways to influence the calculation of the cache key, and it would make more sense to just let you define a "calculate the cache key" hook instead of providing more ad hoc tweaks to the existing one. Happy to review and merge a PR that does this more general change (which would also let us deprecate extraCacheKeyData and drop it in AS4) rather than this more specific one.

glasser avatar Jul 06 '22 17:07 glasser

We released a similar feature (generateCacheKey) in AS v3.10.0. Thanks for the inspiration!

glasser avatar Oct 19 '22 00:10 glasser