The purpose of using the cache is to allow the application to respond to user input more quickly and to run the program efficiently. Sometimes we need to cache the data obtained by the remote web server to reduce multiple requests to the same url.
We can use the NSURLCache class in the sdk for memory caching. NSURLRequest requires a cache parameter to describe how the requested url caches data. Let's take a look at its CachePolicy type.
1. NSURLRequestUseProtocolCachePolicy NSURLRequest default cache policy, which is defined using the Protocol.
2. NSURLRequestReloadIgnoringCacheData ignores the cache and downloads it directly from the original address.
3. NSURLRequestReturnCacheDataElseLoad can be downloaded from the original address only when no data exists in the cache.
4. NSURLRequestReturnCacheDataDontLoad only uses the cache data. If the cache does not exist, the request fails. It is used when no offline network connection mode is established;
5. NSURLRequestReloadIgnoringLocalAndRemoteCacheData: ignores local and remote cache data and downloads it directly from the original address, similar to NSURLRequestReloadIgnoringCacheData.
6. NSURLRequestReloadRevalidatingCacheData: verify whether the local data is the same as the remote data. If the local data is different, download the remote data. Otherwise, use the local data.
NSURLCache also provides many methods to facilitate the caching of applications. The following example shows how to reduce multiple requests to the same url. See 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 the cache size to 1 MB */
- [UrlCache setMemoryCapacity: 1*1024*1024];
- // Create an nsurl
- NSURL * url = [NSURL URLWithString: paramURLAsString];
- // Create a request
- NSMutableURLRequest * request =
- [NSMutableURLRequest
- RequestWithURL: url
- CachePolicy: NSURLRequestUseProtocolCachePolicy
- TimeoutInterval: 60366f];
- // Obtain the cache output from the request
- NSCachedURLResponse * response =
- [UrlCache cachedResponseForRequest: request];
- // Determine whether a cache exists
- If (response! = Nil ){
- NSLog (@ "get data from cache if cache output exists ");
- [Request setCachePolicy: NSURLRequestReturnCacheDataDontLoad];
- }
- Self. connection = nil;
- /* Create NSURLConnection */
- NSURLConnection * newConnection =
- [[NSURLConnection alloc] initWithRequest: request
- Delegate: self
- StartImmediately: YES];
- Self. connection = newConnection;
- [NewConnection release];
- }
In this example, the request url is www.baidu.com. If the url is cached, we can directly obtain data from the cache. Otherwise, we can retrieve data from www.baidu.com. We set the cache size to 1 MB.
Use the following code to print out the request process:
- -(Void) connection :( NSURLConnection *) connection
- DidReceiveResponse :( NSURLResponse *) response {
- NSLog (@ "receive output ");
- }
- -(NSURLRequest *) connection :( NSURLConnection *) connection
- WillSendRequest :( NSURLRequest *) request
- RedirectResponse :( NSURLResponse *) redirectResponse {
- NSLog (@ "request to be sent ");
- Return (request );
- }
- -(Void) connection :( NSURLConnection *) connection
- DidReceiveData :( NSData *) data {
- NSLog (@ "Accept data ");
- NSLog (@ "data length = % lu", (unsigned long) [data length]);
- }
- -(NSCachedURLResponse *) connection :( NSURLConnection *) connection
- WillCacheResponse :( NSCachedURLResponse *) cachedResponse {
- NSLog (@ "");
- Return (cachedResponse );
- }
- -(Void) connectionDidFinishLoading :( NSURLConnection *) connection {
- NSLog (@ "request completed ");
- }
- -(Void) connection :( NSURLConnection *) connection
- DidFailWithError :( NSError *) error {
- NSLog (@ "request failed ");
- }
When we click the button on the interface for the first time, the output is as follows:
- 18:50:24. 910 Caching [3971: 207] The request will be sent soon
- 18:50:28. 557 Caching [3971: 207] will receive the output
- 18:50:31. 677 Caching [3971: 207] accept data
- 18:50:31. 681 Caching [3971: 207] Data Length = 4414
- 18:50:31. 682 Caching [3971: 207] accept data
- 18:50:31. 682 Caching [3971: 207] Data Length = 2996
- 18:50:38. 107 Caching [3971: 207]
- 18:50:38. 109 Caching [3971: 207] request completed
After reading the button on the page for the second time, the print result is as follows:
- 18:52:18. 894 Caching [3971: 207] The request will be sent soon
- 18:52:18. 895 Caching [3971: 207] will receive the output
- 18:52:18. 895 Caching [3971: 207] accept data
- 18:52:18. 896 Caching [3971: 207] Data Length = 7410
- 18:52:18. 896 Caching [3971: 207] request completed
We can see that there is no "outputs the cache" item. The requested data is the accumulation of the first request, that is, the second request is to obtain data from the memory.