Memory cache We can use the Nsurlcache class in the SDK. Nsurlrequest needs a cache parameter to describe the URL it requested to cache the data, we first look at its cachepolicy type.
1, nsurlrequestuseprotocolcachepolicy nsurlrequest default cache policy, using the Protocol protocol definition.
2, Nsurlrequestreloadignoringcachedata ignore the cache to download directly from the original address.
3, Nsurlrequestreturncachedataelseload only in the cache does not exist when data from the original address download.
4, Nsurlrequestreturncachedatadontload only use the cache data, if there is no cache, the request failed, for the network connection is not established offline mode;
5, Nsurlrequestreloadignoringlocalandremotecachedata: Ignore local and remote cache data, directly from the original address download, Similar to the nsurlrequestreloadignoringcachedata.
6, Nsurlrequestreloadrevalidatingcachedata: Verify that local data and remote data are the same, if different then download remote data, otherwise use local data.
Nsurlcache also provides a number of ways for us to implement the caching mechanism for our applications. Let me illustrate by an example that this example reduces our request for the same URL multiple times. Look at the following code:
-(Ibaction) buttonpress: (ID) sender { nsstring * paramurlasstring= @ "http://www.baidu.com/"; if ([paramurlasstring length] == 0) { nslog (@ "Nil or empty url is given "); return; } nsurlcache *urlcache = [NSURLCache sharedURLCache]; /* Set Cache size to 1m*/ [urlCache setMemoryCapacity:1*1024*1024]; // Create a nsurl nsurl *url = [nsurl urlwithstring: paramurlasstring]; //Create a request nsmutableurlrequest *request = [NSMutableURLRequest requestWithURL:url cachepolicy: nsurlrequestuseprotocolcachepolicy timeoutInterval:60.0f]; //get cached output from request nscachedurlresponse * response = [urlCache cachedResponseForRequest:request]; //to determine if there is a cache if (response != nil) { nslog (@ "If you have cached output, get data from cache"); [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; } self.connection = nil; /* Creatensurlconnection*/ NSURLConnection *newConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; self.connection = newConnection; [newConnection release]; }
In this example, we request a URL for a www.baidu.com Web site. If the URL is cached, we get the data directly from the cache, or we retrieve the data from the www.baidu.com site. We set the cache size to 1M.
Using the following code, I print the requested procedure:
- (void) connection: (nsurlconnection *) connection didreceiveresponse: (nsurlresponse *) response{ NSLog (@ "will receive output"); } - (nsurlrequest *) Connection: (nsurlconnection *) connection willsendrequest: (nsurlrequest *) request redirectresponse: ( nsurlresponse *) redirectresponse{ nslog (@ "outgoing request"); return (Request); } - (void) connection: (nsurlconnection *) connection     didreceivedata: (nsdata *) data{ nslog (@ "Accept data"); nslog (@ "Data length is = %lu", (Unsigned long ) [Data length]); } - (NSCachedURLResponse *) Connection: (nsurlconnection *) connection willcacheresponse: ( nscachedurlresponse *) cachedresponse{ nslog (@ "Cache output" ); return (cachedresponse); } - (void) connectiondidfinishloading: (nsurlconnection *) connection{ nslog (@ "Request Complete"); } - (void) connection: (nsurlconnection *) connection Didfailwitherror: (nserror *) error{ nslog (@ "request failed"); }
When we clicked the button on the interface for the first time, the results were printed as follows:
2011-07-30 18:50:24.910 caching[3971:207] is about to send a request 2011-07-30 18:50:28.557 caching[3971:207] will receive output 2011-07-30 18:5 0:31.677 caching[3971:207] Accept data 2011-07-30 18:50:31.681 caching[3971:207] Data length = 4414 2011-07-30 18:50:31.682 C ACHING[3971:207] Accept data 2011-07-30 18:50:31.682 caching[3971:207] Data length = 2996 2011-07-30 18:50:38.107 Caching[397 1:207] The cache output 2011-07-30 18:50:38.109 caching[3971:207] request completes
After looking at our second click on the button on the interface, print the results as follows:
2011-07-30 18:52:18.894 caching[3971:207] is about to send a request 2011-07-30 18:52:18.895 caching[3971:207] will receive output from 2011-07-30 18:52:18.89 5 caching[3971:207] Accept data 2011-07-30 18:52:18.896 caching[3971:207] Data length = 7410 2011-07-30 18:52:18.896 Caching[3971:207 ] Request Complete