https://kotlinlang.org logo
#ktor
Title
# ktor
r

rsetkus

10/13/2020, 10:28 AM
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

Marc Knaup

10/13/2020, 10:32 AM
What do you mean by implementing? What needs to be implemented? You can enable an in-memory cache using
install(HttpCache)
.
r

rsetkus

10/13/2020, 10:34 AM
It seems it is disabled for some of HttpEngines https://youtrack.jetbrains.com/issue/KTOR-660
m

Marc Knaup

10/13/2020, 10:36 AM
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

rsetkus

10/13/2020, 10:44 AM
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

Marc Knaup

10/13/2020, 10:51 AM
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

rsetkus

10/13/2020, 11:00 AM
Thanks @Marc Knaup. For now, I think I know where to start. 🙂
👍 1
m

Marc Knaup

10/13/2020, 11:07 AM
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.
401 Views