Skip to content
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

apollo-server-redis SET method called with ttl 0 #1306

Closed
raduachim opened this issue Jul 6, 2018 · 6 comments
Closed

apollo-server-redis SET method called with ttl 0 #1306

raduachim opened this issue Jul 6, 2018 · 6 comments
Assignees

Comments

@raduachim
Copy link
Contributor

Hi,

I am trying to understand how to use apollo-server-redis for caching calls to a REST api.
For me it's a bit confusing from the docs how the flow would work.

For the graphql server I am using:

  • "apollo-datasource-rest": "^2.0.0-rc.6",
  • "apollo-server-lambda": "^2.0.0-rc.6",
  • "apollo-server-redis": "^2.0.0-rc.6",

I have a simple Redis inside a container and have configured my apollo server with:

cache: new RedisCache({
    host: '127.0.0.1'
  })

On my type in the schema I have added a @cacheControl(maxAge: 600) and then I tried my query only to receive the error: "message": "ERR invalid expire time in set",

Digging a bit in the apollo-server-redis code I see that the set method is called with options that has a key ttl with value 0.

After some more experimenting I see that I need to set in my REST api in the response header the header cache-control: max-age=1337. Might be a stupid question, but why do I need to do this?

And after configured my REST api to return this cache-control header, the set method in the apollo-server-redis is being called with a float value, not 1337 but rather 1336.24 or something similar.
Which also breaks when trying to set something in Redis. So it seems to need some parseInt

@clarencenpy
Copy link
Contributor

Hi @raduachim! If you are using the Datasources API on top of a REST API that already has standard cache-control headers set, these will be passed through directly to apollo-server-redis. That is one of the cool benefits of wrapping REST with datasources, and there is no need to specify @cacheControl(maxAge: 600).

I suspect there are some rounding issues with converting milliseconds to seconds while parsing the cache-control header, will look into that!

@raduachim
Copy link
Contributor Author

Thanks for the response @clarencenpy

So just to make sure I understand this correctly - this works only if the REST API has the cache-control headers set?

When the headers are not set I can see that the redis set method is called with ttl: 0

@raduachim
Copy link
Contributor Author

Hi

I think I understand now how it should work.
The REST API should reply with the cache-control header.

I've looked a bit in the code and what I can tell is that the age function from http-cache-semantics is returning the float value.

A parseInt on let ttl = policy.timeToLive() / 1000; inside the HTTPCache from apollo-datasource-rest should fix it. I tried looking if I can create a PR for it but it seems a bit difficult to test it since time is mocked and I am unsure if it should be fixed by this package or in that external dependency?

@clarencenpy
Copy link
Contributor

Hi @raduachim, thanks for digging in on this! It seems like the easiest fix would be to parseInt on policy.timeToLive(). If that one line change fixes up your issue nicely, I think merging it in as a PR would be not be a problem at all (since changes are minimal).

raduachim pushed a commit to raduachim/apollo-server that referenced this issue Jul 10, 2018
@raduachim
Copy link
Contributor Author

@clarencenpy Thanks. I realized it should be a Math.round rather than a parseInt.
I hope I've created the PR to the correct branch.

@clarencenpy
Copy link
Contributor

PR merged, closing issue. Thanks @raduachim for working on this!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants