日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ios url缓存策略——NSURLCache、 NSURLRequest

發(fā)布時(shí)間:2024/8/23 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios url缓存策略——NSURLCache、 NSURLRequest 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一:url 緩存策略

?

NSURLRequest

?

?

requestWithURL:cachePolicy:timeoutInterval:

?

1:NSURLRequestUseProtocolCachePolicy


This flag will use the underlying protocol’s caching mechanism if the protocol
supports it.

?

基礎(chǔ)策略

?

2:NSURLRequestReloadIgnoringLocalCacheData


This flag specifies that the local cached copy of the resource that is about to be
downloaded must be disregarded and the remote cache policy must be effective.
If there is a local copy of the resource, managed by the framework itself, it will be
ignored.

?

忽略本地緩存

?

?

3:NSURLRequestReturnCacheDataElseLoad

?


This flag specifies that the cached data must be used before attempting to load the
data from the original source. The cached data could be protocol-based cached or
locally cached. If there is no cached data, the data will be downloaded from the
original source.

?

首先使用緩存,如果沒(méi)有本地緩存,才從原地址下載

?

?

4:NSURLRequestReturnCacheDataDontLoad


This flag specifies that only the local cached data must be used. If the data has not
been cached, the request will fail. This is a great flag to use whenever your application
wants to perform operations in offline mode (such as the Offline Mode in
web browsers).

?

使用本地緩存,從不下載,如果本地沒(méi)有緩存,則請(qǐng)求失敗。此策略多用于離線操作

?

5:NSURLRequestReloadIgnoringLocalAndRemoteCacheData


This flag disregards any type of caching involved in the process, local and remote,
and always attempts to download the data from the original source.

?

無(wú)視任何的緩存策略,無(wú)論是本地的還是遠(yuǎn)程的,總是從原地址重新下載

?

?

6:NSURLRequestReloadRevalidatingCacheData

?


This flag specifies that the original source of data must validate the local cache (if
any) before an attempt is made to download the data from the original source. If
there is a copy of the original source cached locally and the remote source specifies
that the cached data is valid, the data won’t be downloaded again. In any other
case, the data will be downloaded from the original source.

?

如果本地緩存是有效的則不下載。其他任何情況都從原地址重新下載

?

?

二:NSURLCache

?

NSURLCache *urlCache = [NSURLCache sharedURLCache];

?

?

In iOS, NSURLCache supports caching data only in memory and not
on disk.


?

(NSURLCache 僅支持內(nèi)存緩存,不支持硬盤(pán)緩存)




[cpp]?view plaincopyprint?
  • -?(void)?downloadURL:(NSString?*)paramURLAsString{??
  • ??
  • if?([paramURLAsString?length]?==?0){??
  • ????NSLog(@"Nil?or?empty?URL?is?given");??
  • ????return;??
  • }??
  • ??
  • /*?Get?the?shared?URL?Cache?object.?No?need?to?create?a?new?one?*/??
  • NSURLCache?*urlCache?=?[NSURLCache?sharedURLCache];??
  • ??
  • /*?We?will?store?up?to?1?Megabyte?of?data?into?the?cache?*/??
  • [urlCache?setMemoryCapacity:1*1024*1024];??
  • ??
  • /*?For?our?request,?we?need?an?instance?of?NSURL?so?let's?retrieve?
  • that?from?the?string?that?we?created?before?*/??
  • NSURL?*url?=?[NSURL?URLWithString:paramURLAsString];??
  • ??
  • /*?And?this?is?our?request?*/??
  • NSMutableURLRequest?*request?=[NSMutableURLRequest?requestWithURL:url?cachePolicy:NSURLRequestUseProtocolCachePolicy?timeoutInterval:60.0f];??
  • ??
  • /*?Try?to?get?a?cached?response?to?our?request.?
  • This?might?come?back?as?nil?*/??
  • NSCachedURLResponse?*response?=[urlCache?cachedResponseForRequest:request];??
  • ??
  • /*?Do?we?have?a?cached?response??*/??
  • if?(response?!=?nil){??
  • ????NSLog(@"Cached?response?exists.?Loading?data?from?cache...");??
  • ????[request?setCachePolicy:NSURLRequestReturnCacheDataDontLoad];??
  • }??
  • ??
  • self.connection?=?nil;??
  • ??
  • /*?Start?the?connection?with?the?request?*/??
  • NSURLConnection?*newConnection?=?[[NSURLConnection?alloc]?initWithRequest:request?delegate:self?startImmediately:YES];??
  • self.connection?=?newConnection;??
  • [newConnection?release];??
  • ??
  • }??
  • [cpp]?view plaincopyprint?
  • //?Create?the?request.??
  • //所構(gòu)建的NSURLRequest具有一個(gè)依賴于緩存響應(yīng)的特定策略,cachePolicy取得策略,timeoutInterval取得超時(shí)值??
  • NSURLRequest?*theRequest=[NSURLRequest?requestWithURL:[NSURL?URLWithString:@"http://www.apple.com/"]??
  • ????????????????????????cachePolicy:NSURLRequestUseProtocolCachePolicy??
  • ????????????????????timeoutInterval:60.0];??
  • //?create?the?connection?with?the?request??
  • //?and?start?loading?the?data??
  • NSURLConnection?*theConnection=[[NSURLConnection?alloc]?initWithRequest:theRequest?delegate:self];??
  • if?(theConnection)?{??
  • ????//?Create?the?NSMutableData?to?hold?the?received?data.??
  • ????//?receivedData?is?an?instance?variable?declared?elsewhere.??
  • ????receivedData?=?[[NSMutableData?data]?retain];??
  • }?else?{??
  • ????//?Inform?the?user?that?the?connection?failed.??
  • }??

  • 轉(zhuǎn)載于:https://www.cnblogs.com/zsw-1993/archive/2013/05/07/4880129.html

    總結(jié)

    以上是生活随笔為你收集整理的ios url缓存策略——NSURLCache、 NSURLRequest的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。