I’m an old hand at many things, and recently picked up React and Redux Toolkit. I’m now approaching Redux Toolkit Query. There are other packages like it, such as TanStack React Query. I suppose that an answer to my question could also apply to use of those libraries.
With RTKQ, I’m told that I can cache the results of my prior fetches. My question is: In an an active multi-user environment, what use scenarios would let me safely use the cached data? Or am I missing something important here?
For example, If a user I fetch is edited by some other operator then my existing cache of that user data is stale. I can guard against that by consistently checking a "last updated" field before committing an update. Or I might be interested in seeing the "number of likes" in a Twitter feed steadily increase, but would be thwarted by caching behavior.
In summary, is caching a highly-advertised, but limited-utility, feature of RTKQ?
2
Answers
"Caching" here also means "keeping data between two rerenders of the same component within 3 seconds". You have to put that data somewhere, as you can’t just request it every render – and that’s a cache.
Of course, you could keep that in local component state, but as soon as you have two components that should display data from the same endpoint, but are not close in your React tree, things become tedious and you have to share them somehow.
So, the two benefits even in that multiuser scenario would be "keeping data for you" and "keeping it at least somewhat consistent".
Of course it’s no magic bullet, but it’s probably much better at it than anything you can write yourself, with only a fraction of the code.
If you don’t want data to stick around after your component unmounts, you can just set that
keepUnusedDataFor
value to a low number.Caching is important when it comes to apps like
Gmail
which has the best user experience .If you handle invalidation & syncing logic of data the best possible way , you can give the user the best experience .
Many popular apps like
Whatsapp
Telegram
Instagram
etc. have caching . Use their logic as an example .