Http Caching - manual cache response
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.
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.
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.
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.
No action for us to take on this.