我想为多个组使用多个缓存方案,例如: 1-这个图像组应该每 60 秒刷新一次。 2-这个图像组应该永远存在,除非出现内存警告。 我不知道如何使用一个库(如 AlamofireImage(或 Kingfisher))实现多个缓存程序。我写了这段代码,但它无法清除文件夹中的过期图像(我不想清除所有缓存文件夹内容):
let downloader = ImageDownloader(name: "shortlived_image_downloader")
let cache = ImageCache(name: "shortlived_cache")
cache.maxCachePeriodInSecond = 60
cell.onPlayingImageView.kf.setImage(with: url,
placeholder: UIImage(named:"Placeholder_S"),
options: [.transition(ImageTransition.fade(0.25)),
.downloader(downloader),
.targetCache(cache)],
progressBlock: nil,
completionHandler: nil)
func clearKFShortLiveCache() {
let cache = ImageCache(name: "shortlived_cache")
cache.clearMemoryCache()
cache.cleanExpiredDiskCache()}
最佳答案
在翠鸟中,
您可以使用以下方法清除不同类型的缓存
:
let cache = ImageCache(name: "shortlived_cache")
cache.clearMemoryCache()
cache.clearDiskCache()
要根据您的要求设置缓存大小和时间
,您可以使用:
cache.maxMemoryCost = YOUR_VALUE
cache.maxCachePeriodInSecond = YOUR_VALUE
cache.maxDiskCacheSize = YOUR_VALUE
图像组应该永远存在,除非发生内存警告:它由 Kingfisher
自动处理。
AutoPurgingImageCache
概念未由 Kingfisher
处理。这由 AlamofireImage
支持。在 Kingfisher
中,您必须自己根据 last access date
处理auto purging
。
关于这些属性和方法的详细介绍,可以引用:
https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet http://cocoadocs.org/docsets/Kingfisher/1.1.2/Classes/ImageCache.html
关于ios - 如何使用 Kingfisher(或 AlamofireImage)手动设置缓存中图像的最大期限并清除过期项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44260849/