Cachy
描述
优雅的线程安全的可过期缓存管理,可以缓存任何对象。支持从服务器获取,单个对象的过期日期,UIImageView 加载等。
安装
Cocoapods
CachyKit 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile
pod 'CachyKit'
然后运行
$ pod repo update
$ pod install
特性
- 异步数据下载和缓存。
- 异步图像下载,缓存和显示。
- 所有对象的独立过期日期/时间。
- 内存和磁盘的多层混合缓存。
- 对缓存行为的细致控制。可自定义过期日期和大小限制。
- 如有需要,强制刷新。
- 独立组件。根据需要分别使用Cachy或CachyLoader系统。
- 可以保存JSON、UIImage、ZIP或AnyObject。
- 对
UIImageView
的视图扩展。 - 加载图像时的指示器。
使用方法
import CachyKit
如果您想下载并缓存文件(JSON、ZIP、UIImage或任何类型的文件),只需使用URL调用即可。
let cachy = CachyLoader()
cachy.loadWithURL(URL(string: "http://your_url_here")!) { [weak self] data, _ in
// Do whatever you need with the data object
}
您也可以使用URLRequest进行缓存。
let cachy = CachyLoader()
let request = URLRequest(url: URL(string: "http://your_url_here")!)
cachy.loadWithURLRequest(request) { [weak self] data, _ in
// Do whatever you need with the data object
}
如果您想为每个对象设置过期日期。
let cachy = CachyLoader()
//(optional) if isRefresh = true it will forcefully refresh data from remote server
//(optional) you can set **expirationDate** according to your need
cachy.loadWithURL(URL(string: "http://your_url_here")!,isRefresh = false,expirationDate = ExpiryDate.everyDay.expiryDate()) { [weak self] data, _ in
// Do whatever you need with the data object
}
清除所有缓存。
CachyLoaderManager.shared.clear()
CachyLoader还提供了UIImageView
扩展。
//(optional) if isShowLoading is true it will show a loading indicator
imageView.cachyImageLoad("your URL", isShowLoading: true, completionBlock: { _, _ in })
它将下载、缓存并将UIImage加载到您的UIImageView中。CachyLoader也是可配置的,通过调用函数CachyLoaderManager.shared.configure()
// All the parametre is optional
// Here if you want set how much much memory/disk should use set memoryCapacity, diskCapacity
// To cache only on memory set isOnlyInMemory which is true by default
// You may set expiry date for all the cache object by setting expiryDate
// Your objects may not be conforming to `NSSecureCoding`, set this variable to `false`
CachyLoaderManager.shared.configure(
memoryCapacity: 1020,
maxConcurrentOperationCount: 10,
timeoutIntervalForRequest: 3,
expiryDate: ExpiryDate.everyWeek,
isOnlyInMemory: true,
isSupportingSecureCodingSaving: false
)
expiryDate参数接收以下值:
- .never 使缓存对象永不过期。
- .everyDay 每日结束时过期。
- .everyWeek 每周过期一次。
- .everyMonth 每月设置一个过期日期。
- .seconds 设置几秒后过期。
不使用CachyLoader
使用CachyLoader是缓存文件的最简单方法,但如果您想要管理您的缓存、同步和线程,CachyKit也支持这一点。
配置
下面是如何设置一些配置选项的示例。
Cachy.countLimit = 1000 // setup total count of elements saved into the cache
Cachy.totalCostLimit = 1024 * 1024 // setup the cost limit of the cache
Cachy.shared.expiration = .everyDay // setup expiration date of each object in the cache
Cachy.shared.isOnlyInMemory = false // will be cached on Memory only or both
添加/检索对象
如果您想添加或检索一个对象,只需遵循以下简单步骤。
//1. Create a CachyObject
let object = CachyObject(value: "HEllo, Worlds", key: "key")
// A given expiry date will be applied to the item
let object2 = CachyObject(value: "HEllo, Worlds", key: "key",expirationDate: ExpiryDate.everyDay.expiryDate())
//2. Add it to the cache
Cachy.shared.add(object: object)
//3. Fetch an object from the cache
let string: String? = Cachy.shared.get(forKey: "key")
联系
在 LinkedIn 上关注和联系我。如果您发现任何问题,只需 提交一个工单。我们也热烈欢迎Pull requests。
贡献
如果您想修复任何问题或改进或添加任何新功能,您非常欢迎。
许可协议
Cachy 使用 MIT 许可协议发布。有关详细信息,请参阅 LICENSE 文件。