Skip to content

💾 This is a Cache framework. It can cache any Codable object, support both value type and reference type.

License

Notifications You must be signed in to change notification settings

NanDotWang/CacheKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CacheKit

This is a homemade Cache framework. It can cache any Codable object, support both value type and reference type.

Features

  • 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.

Usage

  • 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 UITableViewCell or UIImageView, 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()
}
  • Cache can also be used separately for caching any other reference or value type object, as long as it confirms to Codable protocol

Example

Todo list

  • Memory eviction: Implement memory evictions upon memory warning notifications.
  • Disk eviction: Implement expirations for disk cache.

Credits

Thanks for those awesome open source projects:

About

💾 This is a Cache framework. It can cache any Codable object, support both value type and reference type.

Resources

License

Stars

Watchers

Forks

Packages

No packages published