iOS Network--nsurlcache setting up the network request cache
Last Update:2015-08-04
Source: Internet
Author: User
<span id="Label3"></p><p><p>today, looking at the HTTP protocol, see the response head of the cache-control, so in-depth study of a bit. Found a Class--nsurlcache class that I have been ignoring in ios.</p></p><p><p></p></p><p><p><span style="font-size: 14pt;">Nsurlcache</span></p></p><p><p>Nsurlcache is used to cache network requests, that is, nsurlrequest, and then cache them according to the Nsurlcache policy we set up.</p></p><p><p>First introduce the various strategies</p></p> <table style="height: 306px; width: 1001px;" border="1" cellspacing="10" cellpadding="10"> <tbody> <tr> <td style="text-align: center;"><span style="font-size: 14pt;">Strategy</span></td> <td style="text-align: center;"><span style="font-size: 14pt;">Significance</span></td> </tr> <tr> <td style="text-align: center;"><p><span style="font-size: 14pt;">Useprotocolcachepolicy</span></p></td> <td style="text-align: center;"><span style="font-size: 14pt;">Default behavior</span></td> </tr> <tr> <td style="text-align: center;"><span style="font-size: 14pt;">Reloadignoringlocalcachedata</span></td> <td style="text-align: center;"><span style="font-size: 14pt;">Do not use cache</span></td> </tr> <tr> <td style="text-align: center;"><span style="font-size: 14pt;">reloadignoringlocalandremotecachedata*</span></td> <td style="text-align: center;"><span style="font-size: 14pt;">I was serious, not using any cache</span></td> </tr> <tr> <td style="text-align: center;"><span style="font-size: 14pt;">Returncachedataelseload</span></td> <td style="text-align: center;"><span style="font-size: 14pt;">Using the cache (regardless of whether it expires), if not in the cache, load it from the network</span></td> </tr> <tr> <td style="text-align: center;"><span style="font-size: 14pt;">Returncachedatadontload</span></td> <td style="text-align: center;"><span style="font-size: 14pt;">Offline Mode: uses the cache (regardless of whether it expires), but <em>does not</em> load from the network</span></td> </tr> <tr> <td style="text-align: center;"><span style="font-size: 14pt;">reloadrevalidatingcachedata*</span></td> <td style="text-align: center;"><span style="font-size: 14pt;">Before using server Authentication</span></td> </tr> </tbody> </table><p><p>Among them reloadignoringlocalandremotecachedata and reloadrevalidatingcachedata two kinds are not realized, can not look.</p></p><p><p>When we create a cache for the request, we choose one of the above strategies, which is</p></p><pre><pre>+ (instancetype) requestwithurl: (nsurl *) URL cachepolicy: (nsurlrequestcachepolicy) cachepolicy timeoutInterval: ( Nstimeinterval) timeoutinterval;</pre></pre><p><p>This method lets us set the policy and time, and then the request is scheduled according to the policy and Time.</p></p><p><p></p></p><p><p><span style="font-size: 14pt;">Feel Nsurlcache</span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;">This uses the default cache policy Returncachedataelseload cache policy,</span></span></p></p><p><p>First you need to create the Nsurlcache class</p></p><pre><pre>Nsurlcache *urlcache = [[nsurlcache alloc] initwithmemorycapacity:<span style="color: #800080;">4</span><span style="color: #800080;">1024x768</span><span style="color: #800080;"></span> diskcapacity: <span style="color: #800080;">0</span> <span style="color: #000000;">diskpath:nil]; [nsurlcache setsharedurlcache:urlcache];</span></pre></pre><p><p>1. Here we can see that we have created 4 * 1024 * 1024 of memory (4MB), without using Disk Space.</p></p><p><p>2.NSURLCache uses [nsurlcache sharedurlcache] to create the default cache behavior, which defaults to 4 (mb) of memory and a (mb) disk space, where we use the custom, so we want to setsharedcache.</p></p><p><p>Then create request and connection to make requests</p></p><pre><pre>Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:<span style="color: #800000;">@ "</span><span style="color: #800000;">http://172.16.25.44/ test1.php</span><span style="color: #800000;">"</span>] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:<span style="color: #800080;">3</span> <span style="color: #000000;"></span> <span style="color: #0000ff;"> Delegate</span><span style="color: #000000;">: self]; [connection start];</span></pre></pre><p><p>Implementing the Nsurlconnectiondelegate Protocol</p></p><pre><pre>-(<span style="color: #0000ff;">void</span>) connectiondidfinishloading: (nsurlconnection *<span style="color: #000000;">) connection{ NSLog (</span><span style="color: #800000;">@ "</span> <span style="color: #800000;"> Finish</span><span style="color: #800000;">"</span><span style="color: #000000;">);}</span></pre></pre><p><p>And then run the request, and the tool here is Charles.</p></p><p><p></p></p><p><p>You can see that there is only one request, and then look at the console output</p></p><pre><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> in</span>:<span style="color: #800080;"><span style="color: #800080;">55.297</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19405</span></span>:<span style="color: #800080;"><span style="color: #800080;">6375355</span></span><span style="color: #000000;"><span style="color: #000000;">] Finish</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> in</span>:<span style="color: #800080;"><span style="color: #800080;">55.301</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19405</span></span>:<span style="color: #800080;"><span style="color: #800080;">6375355</span></span><span style="color: #000000;"><span style="color: #000000;">] Finish</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> in</span>:<span style="color: #800080;"><span style="color: #800080;">55.310</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19405</span></span>:<span style="color: #800080;"><span style="color: #800080;">6375355</span></span><span style="color: #000000;"><span style="color: #000000;">] Finish</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> in</span>:<span style="color: #800080;"><span style="color: #800080;">55.451</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19405</span></span>:<span style="color: #800080;"><span style="color: #800080;">6375355</span></span><span style="color: #000000;"><span style="color: #000000;">] Finish</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> in</span>:<span style="color: #800080;"><span style="color: #800080;">55.618</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19405</span></span>:<span style="color: #800080;"><span style="color: #800080;">6375355</span></span><span style="color: #000000;"><span style="color: #000000;">] Finish</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> in</span>:<span style="color: #800080;"><span style="color: #800080;">55.784</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19405</span></span>:<span style="color: #800080;"><span style="color: #800080;">6375355</span></span><span style="color: #000000;"><span style="color: #000000;">] Finish</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> in</span>:<span style="color: #800080;"><span style="color: #800080;">55.984</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19405</span></span>:<span style="color: #800080;"><span style="color: #800080;">6375355</span></span><span style="color: #000000;"><span style="color: #000000;">] Finish</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> in</span>:<span style="color: #800080;"><span style="color: #800080;">56.120</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19405</span></span>:<span style="color: #800080;"><span style="color: #800080;">6375355</span></span>] Finish</pre><p><p>So multiple requests only make one request, because in memory Nsurlcache caches a copy of the response for us, and the cache is used once the same request is Made.</p></p><p><p></p></p><p><p><span style="font-size: 14pt;">Cache persistence</span></p></p><p><p><span style="font-size: 14px;">Cache if set local disk will automatically persist for us, modify Nsurlcache Create code</span></p></p><pre><pre> Nsurlcache *urlcache = [[nsurlcache alloc] initwithmemorycapacity:<span style="color: #800080;">4</span><span style="color: #800080;">1024x768</span><span style="color: #800080;"></span> diskcapacity: <span style="color: #800080;"></span> - <span style="color: #800080;">1024x768</span> <span style="color: #800080;">1024x768</span> <span style="color: #000000;">diskpath:nil]; [nsurlcache setsharedurlcache:urlcache];</span></pre></pre><p><p>Set a 20MB local disk, and then run the program, making a request discovery or a request without changes. But at the time of the run the application will find that a remote request will not be done!</p></p><p><p>Open sandbox, found in library/caches/bundleid+ project Name/below there are three files</p></p><p><p></p></p><p><p>This is not sqlite! The original Nsurlcache help us to use SQLite to save the request to the database, and then when the same request will be called cache!</p></p><p><p>Can think of WebView if loading a static page does not have to use the request only once, and when the effect to update the remote request will be how cool!</p></p><p><p></p></p><p><p><span style="font-size: 14pt;">Default Policy</span></p></p><p><p><span style="font-size: 14px;">The default policy is that Useprotocolcachepolicy literally means using protocol caching policies, But what is a protocol caching strategy?</span></p></p><p><p><span style="font-size: 14px;">In the response header of the HTTP protocol, there is a field called cache-control, which is used by the server to tell the client how to use the Cache.</span></p></p><p><p><span style="font-size: 14px;">Here is a response head</span></p></p><p><p><span style="font-size: 14px;"></span></p></p><p><p></p></p><p><p>You can see that the behavior specified by Cache-control is public,max-age=5</p></p><p><p>Here's a description of the various Directives.</p></p><p><p></p></p><p><p>corresponding to the table above, it can be seen that the response header is required to cache all content, cache 5 Seconds to expire, 5 seconds to request a remote Server.</p></p><p><p>The corresponding PHP is the header ("cache-control:public,max-age=5");</p></p><p><p></p></p><p><p><span style="font-size: 14pt;">Fake response</span></p></p><p><p><span style="font-size: 14px;">If we want to make some requests and have a specific response, we can make the response ourselves.</span></p></p><pre>NSLog (<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, YES) lastobject]); Nsurlrequest</span></span>*request = [nsurlrequest Requestwithurl:[nsurl urlwithstring:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">http://172.16.25.44/test1.php</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>] Cachepolicy:nsurlrequestreturncachedataelseload Timeoutinterval:<span style="color: #800080;"><span style="color: #800080;">3</span></span><span style="color: #000000;"><span style="color: #000000;">]; Nsurlcache</span></span>* Cache =<span style="color: #000000;"><span style="color: #000000;">[nsurlcache sharedurlcache]; NSData</span></span>*contentdata = [<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">123</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">datausingencoding:nsutf8stringencoding]; Nsurlresponse</span></span>*response = [[nsurlresponse alloc] initwithurl:[nsurl urlwithstring:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">http://172.16.25.44/test1.php</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>] MIMEType:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">text/html</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>Expectedcontentlength:<span style="color: #800080;"><span style="color: #800080;"></span> +</span>Textencodingname:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">UTF-8</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Nscachedurlresponse</span></span>*cacherespone =<span style="color: #000000;"><span style="color: #000000;">[[nscachedurlresponse alloc] initwithresponse:response data:contentdata]; [cache Storecachedresponse:cacherespone forrequest:request]; Connection</span></span>= [[nsurlconnection alloc] initwithrequest:request<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span><span style="color: #000000;"><span style="color: #000000;">: self]; [connection start];</span></span></pre><p><p>As the code above, a response to the @ "http://172.16.25.44/test1.php" request is created and the cache is stored for that Response.</p></p><p><p>Realize</p></p><pre><pre>-(<span style="color: #0000ff;">void</span>) connection: (nsurlconnection *) connection didreceivedata: (nsdata *<span style="color: #000000;">) data{ </span>*datastring = <span style="color: #000000;">[[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; NSLog (</span><span style="color: #800000;">@ "</span><span style="color: #800000;">%@</span><span style="color: #800000;">"</span><span style="color: #000000;">, datastring);}</span></pre></pre><p><p>The output is as follows</p></p><pre><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>:<span style="color: #800080;"><span style="color: #800080;">58.825</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19503</span></span>:<span style="color: #800080;"><span style="color: #800080;">6441561</span></span>]<span style="color: #800080;"><span style="color: #800080;">123</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>:<span style="color: #800080;"><span style="color: #800080;">58.826</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19503</span></span>:<span style="color: #800080;"><span style="color: #800080;">6441561</span></span><span style="color: #000000;"><span style="color: #000000;">] Finish</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>:<span style="color: #800080;"><span style="color: #800080;">58.983</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19503</span></span>:<span style="color: #800080;"><span style="color: #800080;">6441561</span></span>]<span style="color: #800080;"><span style="color: #800080;">123</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>:<span style="color: #800080;"><span style="color: #800080;">58.984</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19503</span></span>:<span style="color: #800080;"><span style="color: #800080;">6441561</span></span><span style="color: #000000;"><span style="color: #000000;">] Finish</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>:<span style="color: #800080;"><span style="color: #800080;">59.167</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19503</span></span>:<span style="color: #800080;"><span style="color: #800080;">6441561</span></span>]<span style="color: #800080;"><span style="color: #800080;">123</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>:<span style="color: #800080;"><span style="color: #800080;">59.167</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19503</span></span>:<span style="color: #800080;"><span style="color: #800080;">6441561</span></span><span style="color: #000000;"><span style="color: #000000;">] Finish</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>:<span style="color: #800080;"><span style="color: #800080;">59.334</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19503</span></span>:<span style="color: #800080;"><span style="color: #800080;">6441561</span></span>]<span style="color: #800080;"><span style="color: #800080;">123</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>-<span style="color: #800080;"><span style="color: #800080;"></span> ,</span>-<span style="color: #800080;"><span style="color: #800080;">Geneva</span></span> <span style="color: #800080;"><span style="color: #800080;"></span> the</span>:<span style="color: #800080;"><span style="color: #800080;"></span> -</span>:<span style="color: #800080;"><span style="color: #800080;">59.335</span></span>requestcache[<span style="color: #800080;"><span style="color: #800080;">19503</span></span>:<span style="color: #800080;"><span style="color: #800080;">6441561</span></span>] Finish</pre><p><p>You can see that the output is our custom 123 instead of the 1 returned by the Server.</p></p><p><p></p></p><p><p>Modify Response Content</p></p><p><p>Modifying response content requires us to implement the Nsurlconnectiondatadelegate protocol and implement</p></p><pre><pre>-(nscachedurlresponse *) connection: (nsurlconnection *) connection willcacheresponse: (nscachedurlresponse *<span style="color: #000000;">) cachedresponse{ </span>*mutabledata =<span style="color: #000000;"> [[cachedresponse data] mutablecopy]; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">Add Data</span> <span style="color: #000000;"> </span> *response =<span style="color: #000000;"> [[nscachedurlresponse alloc] initWithResponse:cachedResponse.response data: mutabledata]; </span> <span style="color: #0000ff;">return</span> <span style="color: #000000;">response;}</span></pre></pre><p><p>The properties that should be nscachedurlresponse are readonly, so we want to add content to create a mutable copy or additions or deletions.</p></p><p><p></p></p><p><p>iOS Network--nsurlcache setting up the network request cache</p></p></span>