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;

相關文章
相關標籤/搜索