AnyDataCache
基于 Realm 数据库的简单数据缓存框架。
特性
- 使用一行代码添加数据。
- 设置过期时间,框架将在过期时间后自动删除数据。
- 设置磁盘使用限制,框架将自动删除旧文件。
- 设置自动删除属性,使框架仅根据您的指示删除数据。
- 支持 iOS 10.0+ 设备。
CocoaPods
PopupDialog 通过 CocoaPods 提供。只需将以下内容添加到您的 Podfile 中
use_frameworks!
target '<Your Target Name>'
pod 'AnyDataCache'
示例用法
以下代码可用于将数据保存到磁盘 -
DataCache.sharedInstance.addData(dataKey: dataKey, data: data, expiryDate: someDateorNil, autoDelete: true, onAdd: nil)
DataKey 可以是 URL 或类似于社交媒体身份(如 Facebook 图用户的 ID)。'N' 是 swift 'Data' 对象。您还可以传递可选参数,如过期日期、自动删除标志、任务完成的闭包。
要读取保存的数据,您可以使用以下代码 -
// if you saved a string as data into DataCache, then this is how you retrieve it.
// Response comes back on main thread after read operation completes on background thread.
DataCache.sharedInstance.getData(dataKey: dataKey) { [weak self] anyData in
if let messageData = anyData?.data {
if let message = String(data: messageData, encoding: .utf8) {
print("Your saved message is : \(message)")
}
}
}
设置磁盘使用限制
DataCache.sharedInstance.setStorageLimitInMB(20) // 20 MB
作者
Vijayendra Tripathi,Baselabs LLP 创始人。在推特上关注:@vijayendra_t
许可
AnyDataCache 在MIT许可证下可用。查阅LICENSE文件了解更多信息。
Floppy图标:由Flat Icons设计,来自 www.flaticon.com
Orbiton字体 - https://fonts.google.com/specimen/Orbitron