okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

Http Caching - manual cache response

Open tcf150 opened this issue 9 years ago • 4 comments

Hello, I am looking for advice/best practice to manual cache my response by using another url. when my response return http isSuccess, i try to edit the Request to another url and store it into cache.

I am using compile 'com.squareup.okhttp3:okhttp:3.2.0' i had try use the code below, but not working. When i try to retrieve the Response in FORCE_CACHE mode, i get http code 504, due to DiskLruCache > lruEntries > Entry > readable is false.

InternalCache responseCache = Internal.instance.internalCache(client); responseCache.put(response);

Hope anyone can give me some advice to do manual cache.

Thanks.

tcf150 avatar Mar 24 '17 15:03 tcf150

Please don’t import code in the internal package; that’s not a stable API and will change from release to release.

There’s no mechanism to manually insert an entry into the cache. If you want that, you might want to make your own interceptor to complement the cache.

swankjesse avatar Mar 24 '17 15:03 swankjesse

Hi Jesse,

can i extend the CacheInterceptor and include my own logic? or you prefer me to build the brand new interceptor by myself?

Thanks.

tcf150 avatar Mar 24 '17 15:03 tcf150

Writing your own cache interceptor is an option, and you can copy+paste CacheInterceptor as a starting point. But that code is going to change dramatically in an upcoming release so it’s unwise to import any code from that package.

My guess is you want something much simpler – possibly in an interceptor or possibly in the code that calls into OkHttp. Our cache is designed to be transparent, getting all of its policy from headers like cache-control. Since your cache has more knowledge you won’t need as much code.

swankjesse avatar Mar 24 '17 16:03 swankjesse

No action for us to take on this.

swankjesse avatar Apr 15 '17 17:04 swankjesse