llfbandit / dio_cache_interceptor

Dio HTTP cache interceptor with multiple stores respecting HTTP directives (ETag, Last-Modified, Cache-Control) with options.
https://pub.dev/packages/dio_cache_interceptor
119 stars 70 forks source link

The cache interceptor changes the HTTP response code from 200 to 304 #141

Closed FixThisWorld closed 10 months ago

FixThisWorld commented 10 months ago

Before using this package, I would get send the same HTTP request to a server twice and get a 200 status both times as response. When using this package, suddenly the second status is 304 'not modified' (and does not contain the requested data).

I guess this package somehow lets the server know that the responses will be cached, so the server just replies that the data did not change, i.e. should be loaded from cache. Though, my application does not receive the cached data with a 200 status, as I would expect. I believe that the responses seen by the application should be exactly the same with or without caching.

This may be related to issue #121?

FixThisWorld commented 10 months ago

I noticed that in _isCacheCheckAllowed(), there is a check for a 304 status, which always returns true. Maybe the problem is that the response of the first request is not yet in the cache, so the cache lookup fails for the second request?

llfbandit commented 10 months ago

The description is unclear to me, the store should be in sync. If the interceptor detects that the second request is already in cache, it will request with cache infos the origin server (in your case), this is why you get status 304. So the data are in the store already. Your questionning about concurrent request is viable but you get a 304 so I don't understand.

The package will always return status 304 from cached response (with the body normally).

Now why you don't get the data from here is a mystery at this time.

Which store do you use? If you use another store like MemCacheStore, are you able to reproduce it? If so, can you provide a simple reproducer?

FixThisWorld commented 10 months ago

Ok. Probably I have a different understanding what a HTTP cache should do. In my understanding, it should reduce the server load and give quicker responses for the client. But the content of the responses should not change in my opinion.

What I expect (and get without cache):

With the cache, the last response yields a 304 status instead of 200. About the data with the 304 status, I'm actually not sure any more. Maybe there was some data?

I am using the MemCacheStore.

Given that apparently I want something different from what this package provides, I now wrote a simple cache interceptor, which suffices for my purposes.