[iOS] NSURLCache && NSCachedURLResponse
来源:互联网 发布:蒙顶山茶叶交易所 知乎 编辑:程序博客网 时间:2024/05/16 19:03
NSURLCache
1. 初始化相关的几个方法:sharedURLCache;setSharedURLCache;initWithMemoryCapacity
sharedURLCache方法返回一个NSURLCache实例。
默认情况下,内存是4M,4* 1024 * 1024;Disk为20M,20 * 1024 * 1024;路径在(NSHomeDirectory)/Library/Caches/(current application name, [[NSProcessInfo processInfo] processName])
setSharedURLCache可以通过这个方法来改变默认的NSURLCache。通过initWithMemoryCapacity来定制自己的NSURLCache。
2. cache使用相关的几个方法:cachedResponseForRequest;storeCachedResponse;removeCachedResponseForRequest;
removeAllCachedResponses
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request;
如果对应的NSURLRequest没有cached的response那么返回nil
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request;
为特定的NSURLRequest做cache
- (void)removeCachedResponseForRequest:(NSURLRequest *)request;
移除特定NSURLRequest的cache
- (void)removeAllCachedResponses;
移除所有的cache
3. property方法
- (NSUInteger)memoryCapacity;
- (NSUInteger)diskCapacity;
- (void)setMemoryCapacity:(NSUInteger)memoryCapacity;
可能会导致内存中的内存被截断
- (void)setDiskCapacity:(NSUInteger)diskCapacity;
- (NSUInteger)currentMemoryUsage;
- (NSUInteger)currentDiskUsage;
4. Misc
a. NSURLCache在每个UIWebView的的NSURLRequest请求中都会被调用。
b. iOS设备上NSURLCache默认只能进行内存缓存。可以通过子类化NSURLCache来实现自定义的版本从而实现在DISK上缓存内容。
c. 需要重写cachedResponseForRequest,这个会在请求发送前会被调用,从中我们可以判定是否针对此NSURLRequest返回本地数据。
d. 如果本地没有缓存就调用下面这条语句:return [super cachedResponseForRequest:request];
NSCachedURLResponse
包装了一下系统缓存机制的对象,保持了缓存对象的个性和特性。
1. NSURLCacheStoragePolicy 缓存策略有三种
enum
{
NSURLCacheStorageAllowed,
NSURLCacheStorageAllowedInMemoryOnly,
NSURLCacheStorageNotAllowed,
};
默认是第一种。不过在iOS的上官方文档上有这么一个解释:
Important: iOS ignores this cache policy, and instead treats it asNSURLCacheStorageAllowedInMemoryOnly.
也就是说iOS上只有内存缓存,没有磁盘缓存。
2. 构造方法
- (id)initWithResponse:(NSURLResponse *)response data:(NSData *)data;
- (id)initWithResponse:(NSURLResponse *)response data:(NSData *)data userInfo:(NSDictionary *)userInfo storagePolicy:(NSURLCacheStoragePolicy)storagePolicy;
3. Open API
- (NSURLResponse *)response;
- (NSData *)data;
- (NSDictionary *)userInfo;
- (NSURLCacheStoragePolicy)storagePolicy;
- [iOS] NSURLCache && NSCachedURLResponse
- NSURLCache && NSCachedURLResponse
- 76.iOS 网络缓存(NSURLCache/NSCachedURLResponse)
- NSURLCache,iOS
- iOS-NSURLCache
- iOS 缓存 NSURLCache类
- iOS -NSURLCache内存缓存
- iOS NSURLCache(网络缓存)
- iOS - 关于NSURLCache
- iOS---Foundation(NSURLCache.h)简介
- NSURLCache
- NSURLCache
- NSURLCache
- NSURLCache
- nsurlcache
- NSURLCache
- NSURLCache
- NSURLCache
- android View 的显示状态GONE,VISIBLE和INVISIBLE区别
- 业务流程图
- cactiez V9.1升级到cactiez V10.1操作步骤
- OWL Web本体语言 指南(中文版)
- asp.net身份验证方式
- [iOS] NSURLCache && NSCachedURLResponse
- source insight快捷键及使用技巧
- 一个很好修改、调整css样式的方法
- 选择(select)模型
- C++类对象内存模型与成员函数调用分析(中)
- ibus输入法"没有输入窗口"的问题
- 苹果的出货量增加还是减少?调研公司意见分歧
- 编程语言关系图谱 谁的影响力更大
- mars老师 [Android视频教学]01_20 XML解析