I am facing issue in setting validity for Cache in android jetpack using coil. What I Need: In Android Jetpack compose, I need to download an SVG image from the Url and store it to the cache. But the cache should store the image only for 20 seconds. so within the 20 seconds, there should not be any further image request. For this 20 seconds, the image should display from the cache. After 20 seconds, I want the image, should get cleared from the cache.
What's the problem I am facing: The image is not getting cleared from the cache even after 20 seconds. I am using Coil for the jetpack compose jetpack coil and Jetpack header for Cachecontrol Thanks in Advance....
In Application Class, I created a single instance ImageLoader
val imageLoader = ImageLoader.Builder(applicationContext)
.componentRegistry {
add(SvgDecoder(applicationContext))
}
.okHttpClient {
OkHttpClient.Builder()
.cache(CoilUtils.createDefaultCache(applicationContext))
.addNetworkInterceptor(provideCacheInterceptor(10))
.build()
}
.memoryCachePolicy(CachePolicy.DISABLED)
.diskCachePolicy(CachePolicy.ENABLED)
.build()
Coil.setImageLoader(imageLoader = imageLoader)
In Moment.class:
val singleInstanceImageLoader = context.imageLoader
val imageRequest = ImageRequest.Builder(context)
.data(decodedURL)
.memoryCachePolicy(CachePolicy.DISABLED)
.diskCachePolicy(CachePolicy.ENABLED)
.addHeader("Cache-Control", "max-age=20,public")
.build()
val imagePainter = rememberCoilPainter(
request = imageRequest,
imageLoader = singleInstanceImageLoader
)
Image(
painter = imagePainter,
contentDescription = null,
modifier = Modifier
.wrapContentSize(Alignment.Center)
.padding(0.dp, 15.dp, 0.dp, 15.dp)
)