This is a homemade Cache framework. It can cache any Codable object, support both value type and reference type.
- Memory cache: Use NSCache as memory cache.
- Disk cache: Thread safe, async disk cache.
- Generic: Support for fetching and caching any Codable.
- Support value types: Support caching of value types.
- UIImageView & UITableViewCell extensions: Provide ready-to-use extesions for downloading and caching remote images.
- Option 1: Use it on any
UIImageView
let imageView = UIImageView(frame: image_view_frame)
imageView.setImage(with: image_url)- Option 2: Use it on
UITableViewCell
Due to the fact that default imageView is managed by UITableViewCell, cell.setNeedsLayout needs to be called to make cell gets updated when retrieving image task finishes, so a convenient extension is added to UITableViewCell to make it easy for this specific usage.
You can just use it in tableView: cellForRowAt indexPath delegate:
cell.setImage(with: image_url)- Both extensions can be used with a placeholder image:
let placeholder = UIImage(named: "placeholder_image")
xxx.setImage(with: url, placeholder: placeholder)- With options:
// Image cache & process options
let options: [Option] = [.cacheInMemoryOnly, .forceRefresh]
cell.setImage(with: url, placeholder: placeholder, options: options)-
Available options:
cacheInMemoryOnly: Only cache object in memory, not in disk.forceRefresh: Force downloading the target from remote resouce, local cache ignored.
-
In addition, if you need more control on each image downloading task associated with
UITableViewCellorUIImageView, you can cancel, suspend and resume associated image downloading task at proper time.
/// Suspend unfinished image downloading task when the cell is disappearing.
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.suspendImageDownloading()
}
/// Resume unfinished image downloading task when the cell is appearing.
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.resumeImageDownloading()
}Cachecan also be used separately for caching any otherreferenceorvaluetype object, as long as it confirms toCodableprotocol
- Memory eviction: Implement memory evictions upon memory warning notifications.
- Disk eviction: Implement expirations for disk cache.
Thanks for those awesome open source projects:
