优秀的线程安全的、可过期的缓存管理工具,可以缓存任何对象。 支持从服务器获取数据、单个对象过期日期、UIImageView 加载等功能。
CachyKit 可通过 CocoaPods 进行安装。 要安装它,只需将以下行添加到您的 Podfile 中
pod 'CachyKit'
然后运行
$ pod repo update
$ pod install
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 参数接受
使用 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")
在 Twitter 或 LinkedIn 上关注和联系我。 如果您发现问题,请打开一个 issue。 也非常欢迎 pull request。
如果您想修复任何问题、改进或添加任何新功能,我们非常欢迎您。
Cachy 在 MIT 许可下发布。 有关详细信息,请参阅 LICENSE。