BRCacheManager 是为您的模型提供的一个简单、高效的基于磁盘的缓存。通常,您可能会通过 AFNetwroking 获取一些内容,将其从 JSON 转换为您自定义的模型并显示。使用 BRCacheManager 缓存您的内容,可以加快应用程序的速度。BRCacheManager 会为您自动管理缓存内容的过期和清理。
(即将推出) 要运行示例项目,请克隆仓库,然后从 Example 目录首先运行 pod install
命令。
AFNetworking 的工作流程示例。
#import <BRCacheManager.h>
+ (void)getMyContent
completionHandler:(CustomBlock)completionHandler
{
NSString *path = @"myendpoint";
//check if we have content in the cache with a max age of 5 minutes
id contents = [[BRCacheManager sharedManager] getCachedContentForKey:path withExpireTimeInSeconds:(60 * 5)];
if (contents) {
return completionHandler(contents, nil);
}
[[CustomSessionManager sharedClient] GET:path
parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject)
{
MyModel *myModel = [self customModelFromJson:responseObject];
//save your content to disk
[[BRCacheManager sharedManager] saveCachedContent:[myModel copy] forKey:path];
return completionHandler(myModel, nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
return completionHandler(nil, error);
}];
}
iOS 7 + ARC。如果要缓存自定义模型,则它必须实现 NSCoding。
向那些做出贡献的人表示特别的感谢。请向开发分支提交所有拉取请求。
Michael Mork - https://github.com/Morkrom
BRCacheManager 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。