compose-imageloader icon indicating copy to clipboard operation
compose-imageloader copied to clipboard

Reset cache

Open pravinlondhe opened this issue 1 year ago • 2 comments

how to reset cache for one particular image? I am using rememberImagePainter with default image loader. If i change image still it shows old/cached image unless i clear app cache.

pravinlondhe avatar Dec 04 '23 13:12 pravinlondhe

If you want to disable read memory or disk cache for a request, you can configure CachePolicy.

val request = ImageRequest { 
    data("")
    options {
        memoryCachePolicy = CachePolicy.WRITE_ONLY // or DISABLED
        diskCachePolicy = CachePolicy.WRITE_ONLY // or DISABLED
    }
}

If you want to keep the cache on, you may need to customise the Keyer.

val request = ImageRequest { 
    data("")
    components {
        add(CustomKeyer())
    }
}

class CustomKeyer : Keyer {
    override fun key(data: Any, options: Options, type: Keyer.Type): String {
        val key = ""
        when (type) {
            Keyer.Type.Memory -> {
                // key
            }
            Keyer.Type.Disk -> {
                // key 
            }
        }
        return key
    }
}

qdsfdhvh avatar Dec 05 '23 02:12 qdsfdhvh

Hi, I saw your email and this is one solution I came up with to your question.

For example, a user avatar is a link like this: https://example.com/user/avatar.jpg,

You can manually add some query parameters to the url that the server doesn't need, like this: https://example.com/user/avatar.jpg?v=1

If your avatar makes an update, update the parameter, just like the: https://example.com/user/avatar.jpg?v=2

this allows the cache to be updated at a more opportune time.

Adding a query to ktor is roughly like this, and there are probably simpler ways to do it.

val url = "https://example.com/user/avatar.jpg"
val imageRequest = ImageRequest {
    data(
        URLBuilder(url).apply {
            parameters.append("v", "1")
        }.build()
    )
}

qdsfdhvh avatar Dec 05 '23 06:12 qdsfdhvh