We're seeing some behaviour where we aren't caching responses in OkHttp, and end up hitting the server every time. However, the response has an Expires time in the future, so ideally it would be cached.
Here's a simple example of headers we're seeing in the response (request was sent and response was received at Sat, 16 Jan 2021 00:40:36 GMT
):
date: Sat, 16 Jan 2021 00:40:36 GMT
age: 6
expires: Sat, 16 Jan 2021 00:40:40 GMT
last-modified: Sat, 16 Jan 2021 00:40:30 GMT
From what I've seen from looking at the CacheStrategy, the problem is that it adds together date + age to see if it's past the expiry time. In this case, 00:40:36 + 6 = 00:40:42 > 00:40:40
, so it ends up not being added to the cache.
So I think ideally, either the response date would be equal to last-modified (in this case Sat, 16 Jan 2021 00:40:30 GMT), or we'd need to have a custom CacheStrategy to use last-modified instead of date for these calculations.
If anyone has any insights into whether I'm making any bad assumptions, or if one of the above options is preferable, please let me know. I've looked at some of the specs for date/age headers and it's a bit unclear to me what they should be in this scenario.
I've also found it a bit difficult to debug the caching behaviour in OkHttp, right now I've just been using conditional breakpoints to try to trace it, but if anyone has a better idea I'd appreciate that as well.
from Caching responses based on date and age headers
No comments:
Post a Comment