gpt4 book ai didi

Android:在jetpack中使用线圈的图像的缓存有效性

转载 作者:行者123 更新时间:2023-12-04 23:44:54 24 4
gpt4 key购买 nike

我在使用线圈为 android jetpack 中的缓存设置有效性时遇到问题。
我需要什么 :在 Android Jetpack compose 中,我需要从 Url 下载 SVG 图像并将其存储到缓存中。但是缓存应该只存储图像 20 秒。所以在 20 秒内,不应该有任何进一步的图像请求。在这 20 秒内,图像应从缓存中显示。 20 秒后,我想要图像,应该从缓存中清除。
我面临什么问题:即使 20 秒后,图像也没有从缓存中清除。我正在使用 Coil 作为喷气背包的组合 jetpack coil Jetpack header for Cachecontrol
提前致谢....
在 Application Class 中,我创建了一个单实例 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)
在 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)

)

最佳答案

我收到了线圈的回复:

This isn't a bug. Looking at your code, you're setting thecache-control header for the request. You need to set it for theresponse using a network interceptor.


但是我尝试了这种方法,它也没有奏效。
希望这可能对其他人有用。

关于Android:在jetpack中使用线圈的图像的缓存有效性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69122657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com