2017-05-30 335 views
1

我想爲多個組使用多個緩存方案,例如: 1 - 此圖像組應該每60秒刷新一次。 2 - 除非發生內存警告,否則此映像組應永久存在。 我不知道如何使用AlamofireImage(或Kingfisher)這樣的庫來實現多個緩存程序。我寫了這個代碼,但它不能在文件夾中明確失效映像(我不想清除所有的緩存文件夾的內容):如何使用Kingfisher(或AlamofireImage)手動設置緩存中的圖像的最大週期並清除過期的項目?

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()} 

回答

1

翠鳥

您可以通過清除cache不同類型:

let cache = ImageCache(name: "shortlived_cache") 

cache.clearMemoryCache() 
cache.clearDiskCache() 

要根據您的要求設置cache size and time,你可以使用:

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

相關問題