UIWebView缓存基础知识

第一. NSURLRequest的缓存设置缓存

- (id)initWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;app

cachePolicy表示缓存策略函数

一、 NSURLRequestUseProtocolCachePolicy = 0,this

默认策略,使用缓存.net

2.NSURLRequestReloadIgnoringLocalCacheData = 1,rest

忽略本地缓存对象

3.NSURLRequestReturnCacheDataElseLoad = 2,内存

若是有缓存,无论过时时间优先使用本地缓存,若是没有本地缓存,才从原地址下载ci

4. NSURLRequestReturnCacheDataDontLoad = 3rem

只使用缓存,若是没有匹配的缓存则报告离线模式,而不会从网上load数据

第2、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.经常使用的几个函数:

//Returns the NSCachedURLResponse stored in the cache with the given request.

(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request;

//Stores the given NSCachedURLResponse in the cache using the given request.

- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request;

// Removes the NSCachedURLResponse from the cache that is stored using the given request.

(void)removeCachedResponseForRequest:(NSURLRequest *)request;

//Clears the given cache, removing all NSCachedURLResponse objects that it stores.

 

- (void)removeAllCachedResponses;

3. property方法

- (NSUInteger)memoryCapacity;

- (NSUInteger)diskCapacity;

- (void)setMemoryCapacity:(NSUInteger)memoryCapacity;

第3、 NSCachedURLResponse

系统缓存对象,保持了缓存对象的个性和特性。

1. NSURLCacheStoragePolicy 缓存策略有三种

enum
{
NSURLCacheStorageAllowed,
NSURLCacheStorageAllowedInMemoryOnly,
NSURLCacheStorageNotAllowed,
};

 

NSURLCacheStorageAllowed

Specifies that storage in NSURLCache is allowed without restriction.

Important: iOS prior to version 5 ignores this cache policy, and instead treats it asNSURLCacheStorageAllowedInMemoryOnly.

 

能够看出,iOS设备上NSURLCache默认只能进行内存缓存。

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;

相关文章
相关标签/搜索