UIWebView緩存基礎知識

第一.NSURLRequest的緩存設置

+(id)initWithURL:(NSURL*)URLcachePolicy:(NSURLRequestCachePolicy)cachePolicytimeoutInterval:(NSTimeInterval)timeoutInterval;

cachePolicy表示緩存策略

1、 NSURLRequestUseProtocolCachePolicy = 0,

默認策略,使用緩存

2.NSURLRequestReloadIgnoringLocalCacheData = 1,

忽略本地緩存

3.NSURLRequestReturnCacheDataElseLoad = 2,

如果有緩存,不管過期時間優先使用本地緩存,如果沒有本地緩存,才從原地址下載

4. NSURLRequestReturnCacheDataDontLoad = 3

只使用緩存,如果沒有匹配的緩存則報告離線模式,而不會從網上load數據

第二、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;

第三、NSCachedURLResponse

系統緩存對象,保持了緩存對象的個性和特性。

1. NSURLCacheStoragePolicy 緩存策略有三種

enum{

NSURLCacheStorageAllowed,

NSURLCacheStorageAllowedInMemoryOnly,

NSURLCacheStorageNotAllowed,

};

NSURLCacheStorageAllowed

Specifies that storage in NSURLCache is allowed without restriction.

Important:iOSprior 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;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容