Skip to content

Using tanstack query for caching in rest APIs #9394

Answered by TkDodo
RobinVdBroeck asked this question in Q&A
Discussion options

You must be logged in to vote

You can definitely use react-query for caching on the server, but note that it doesn’t persist per anywhere default (it’s in memory only), so it doesn’t really compare with redis. It’s also not distributed and probably not optimized specifically for this use-case.

What you can do is combine a QueryClient with persistence and a server storage solution like redis:

  1. when a request comes in, create a new, request-scoped QueryClient
  2. put data into the cache with dehydrate by reading from e.g. redis
  3. perform actions you want
  4. when the request ends, serialize the client with hydrate and write it to redis.

then, when the next request comes in, it will restore from redis and you’d basically have a f…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@RobinVdBroeck
Comment options

@TkDodo
Comment options

Answer selected by RobinVdBroeck
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants