Hi. Did anybody successfully implemented http cach...
# ktor
r
Hi. Did anybody successfully implemented http cache-control on Ktor client? If yes, then I'll much appreciate if you could point me to any source (articles, documentation, code samples etc.) of implementation.
m
What do you mean by implementing? What needs to be implemented? You can enable an in-memory cache using
install(HttpCache)
.
r
It seems it is disabled for some of HttpEngines https://youtrack.jetbrains.com/issue/KTOR-660
m
The issue means that the HTTP requests ignore iOS’ own cache. You can still use
HttpCache
which is Ktor’s own implementation, but in-memory only.
NSURLRequestReloadIgnoringCacheData
may break revalidation calls though. I’m not sure about that. It should not.
r
Okay. So by default, only in memory cache is supported by Ktor. Since we'll be using Ktor in context of mobile applications (Kotlin Multiplatform), we're looking for more persistent cache (disk storage etc.) option. I'd guess we'd need to write our own Ktor feature for it.
m
Yes, when using
HttpCache
you can configure it to use your own
HttpCacheStorage
implementation. That implementation needs to load & save requests as told by
HttpCache
. So you build on top of
HttpCache
instead of re-inventing it 🙂
r
Thanks @Marc Knaup. For now, I think I know where to start. 🙂
👍 1
m
I just saw that there’s an issue with implementing your own cache. https://youtrack.jetbrains.com/issue/KTOR-501 You’ll have to create
HttpCacheEntry
instances when loading data from disk but its constructor is internal for some reason. As a workaround you could force the usage of the internal constructor (
@Suppress("INVISIBLE_MEMBER")
- it may break in future Ktor updates) and engage in the ticket to get this improved.
I’ve also added https://youtrack.jetbrains.com/issue/KTOR-1135 There may be ways around that but the implementation is less clean then 😕
When in doubt, just copy & paste
HttpCache
for now and use the official version again once it was improved.
485 Views