How to set max period on image in cache and clear expired items manually using Kingfisher(or AlamofireImage)?

1.4k Views Asked by At

I would like to use multiple cache scenarios for multiple groups like: 1-this image group should exist refresh every 60 seconds. 2-this image group should exist forever unless memory warning occurs. I don't know how to implement multiple cache programs with one library like AlamofireImage(or Kingfisher). I wrote this code but it can not clear expired images in folder(I dont want to purge all the cache folder content):

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

There are 1 best solutions below

0
On

In Kingfisher,

You can clear different types of cache using:

let cache = ImageCache(name: "shortlived_cache")

cache.clearMemoryCache()
cache.clearDiskCache()

To set cache size and time according to your requirement, you can use:

cache.maxMemoryCost = YOUR_VALUE
cache.maxCachePeriodInSecond = YOUR_VALUE
cache.maxDiskCacheSize = YOUR_VALUE

Image group should exist forever unless memory warning occurs: It is handled automatically by Kingfisher.

AutoPurgingImageCache concept is not handled by Kingfisher. This is supported by AlamofireImage. In Kingfisher, you have to handle auto purging based on last access date yourself.

For details on these properties and methods, you can refer to:

https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet http://cocoadocs.org/docsets/Kingfisher/1.1.2/Classes/ImageCache.html