Kamel-Media / Kamel

Kotlin asynchronous media loading and caching library for Compose.
Apache License 2.0
636 stars 24 forks source link

Disk cache doesn't work on iOS and Android #103

Closed ilya-savinkov closed 4 months ago

ilya-savinkov commented 5 months ago

Hello, I have next Kamel config

KamelConfig {
    takeFrom(KamelConfig.Default)
    svgCacheSize = 300

    httpFetcher {
        httpCache(DefaultHttpCacheSize)

        Logging {
            level = LogLevel.INFO
            logger = object : Logger {
                override fun log(message: String) {
                    Napier.d(
                        message = message,
                        tag = "kamel"
                    )
                }
            }
        }
    }
}

Here I have httpCache with DefaultHttpCacheSize. When I launch app my images download, but when I restart app my images download again.

---------------------------- PROCESS STARTED (13037) for package com.intellect.logos ----------------------------
2024-05-06 09:37:24.553  D  REQUEST: http://192.168.0.104:8080/image/US.svg
                            METHOD: HttpMethod(value=GET)
2024-05-06 09:37:24.554  D  REQUEST: http://192.168.0.104:8080/image/EU.svg
                            METHOD: HttpMethod(value=GET)
2024-05-06 09:37:24.789  D  RESPONSE: 200 OK
                            METHOD: HttpMethod(value=GET)
                            FROM: http://192.168.0.104:8080/image/EU.svg
2024-05-06 09:37:24.794  D  RESPONSE: 200 OK
                            METHOD: HttpMethod(value=GET)
                            FROM: http://192.168.0.104:8080/image/US.svg
---------------------------- PROCESS ENDED (13037) for package com.intellect.logos ----------------------------
---------------------------- PROCESS STARTED (13158) for package com.intellect.logos ----------------------------
2024-05-06 09:37:38.806  D  REQUEST: http://192.168.0.104:8080/image/US.svg
                            METHOD: HttpMethod(value=GET)
2024-05-06 09:37:38.810  D  REQUEST: http://192.168.0.104:8080/image/EU.svg
                            METHOD: HttpMethod(value=GET)
2024-05-06 09:37:38.921  D  RESPONSE: 200 OK
                            METHOD: HttpMethod(value=GET)
                            FROM: http://192.168.0.104:8080/image/US.svg
2024-05-06 09:37:38.925  D  RESPONSE: 200 OK
                            METHOD: HttpMethod(value=GET)
                            FROM: http://192.168.0.104:8080/image/EU.svg
luca992 commented 4 months ago

So we are actually using ktor's CacheStorage for caching images to the disk, meaning requests will probably still be shown in the logs.

Can you verify there is actually no data in your application's cache directory? I just tested on android & desktop and cached data is being retrieved from the disk cache. Loading cached images in airplane mode in the sample is working as expected on android.

I did find a bug where desktop doesn't work offline, but when online it is still loading images from the disk cache.

luca992 commented 4 months ago

I tested your logging setup. It does indeed log requests even if loading from the disk cache in airplane mode. If you don't like this behavior report it to ktor.

luca992 commented 4 months ago

I tested ios offline, and it also errors before it tries to load from the diskcache. I'll have to look into that as well. It should be working fine while connected to the internet as well though

luca992 commented 4 months ago

If loading data while offline is what you're having an issue with follow #104