Replies: 3 comments 4 replies
-
Hey @JWW87 👋 During the TTL (Time to Live) Period: The data is considered fresh, and any request within this period will return the data from the cache without hitting the database. In your case, this period is set to 15 seconds. After TTL Expires and Within SWR Time: Once the TTL expires, the data enters the SWR phase, which lasts for 60 seconds in your setup. During this time, the cache will still serve the stale (now technically expired) data for any requests. Simultaneously, it will asynchronously refresh the data in the background. Once the data is refreshed in the cache, the updated data will be sent. I hope this helps. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your answer @nurul3101! Looks like I don't fully understand the purpose of SWR. IMHO there's not much difference between TTL and SWR the way you describe it. I refactored my code to use the NextJS data caching layer instead of SWR as a workaround for my use case. Thanks again! |
Beta Was this translation helpful? Give feedback.
-
Team I strongly believe you need reconsider this feature and re do it. At this point your caching strategy is not so useful for practical applications and I am sure most of your customers are suffering from this without even realising it. "Stale While Refresh" means what it says, i.e. the data is stale but we are refreshing it and it will be fresh soon, perhaps within the next few moments. It does not mean that you still get to see the old stale data until it expires. That makes no sense. This is effectively TTL. I just did a couple of tests and with |
Beta Was this translation helpful? Give feedback.
-
Question
Hi all.
I have recently started a NextJS project with Prisma Accelerate. I managed to get it to work quite easily and I'm very happy with it, the performance is really good.
However, I'm having trouble with the SWR cache strategy. The way I understand it from the docs, stale data is served from cache during the SWR lifetime but is refreshed asynchronously. I would assume that refreshed data would be served next time the request is done, but I can't get this to work.
How to reproduce (optional)
If I run this query (simplified for readability) I get user session data and data about the accelerate cache:
After the first request, the cacheInfo is:
And after the second request it's:
After 15 seconds:
After altering the data in the database and still within the 60 seconds of the SWR lifetime, the data is this:
Subsequent requests all look like this but with a different requestId. No matter how many time I request it, the stale data from before altering of the database is being served. After the SWR lifetime has expired, the cacheStatus is 'miss' and the new data is being shown.
Expected behavior (optional)
I expect the new data is shown the next time an SWR request serves the stale data and updates the data from the database. Is this a bug or am I missing something in the way SWR works?
Information about Prisma Schema, Client Queries and Environment (optional)
OS: Windows 11 WSL (Ubuntu)
Database: MySQL
Node.js version: 21.7.1
Run
prisma -v
to see your Prisma version and paste itBeta Was this translation helpful? Give feedback.
All reactions