Cache for iOS development (1): Memory Cache

Source: Internet
Author: User

FrontArticleThis article describes how to upload and download files. It describes how to cache files on iOS devices.

This article will only introduce caching content into the memory, and the next article will introduce caching content on the IOS disk.

The purpose of using the cache is to use the applicationProgramIt can respond to user input more quickly and 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 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 * url = [nsurl urlwithstring: paramurlasstring]; // create a request nsmutableurlrequest * request = [nsmutableu Rlrequest requestwithurl: URL cachepolicy: nsurlrequestuseprotocolcachepolicy timeoutinterval: 60366f]; // obtain the cache from the request. Output nscachedurlresponse * response = [urlcache cachedresponseforrequest: Request] // determine whether there is a cache 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 (@ "");}-(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] will send the request 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 = 44142011-07-30 18:50:31. 682 caching [3971: 207] accept data 18:50:31. 682 caching [3971: 207] Data Length = 29962011-07-30 18:50:38. 107 caching [3971: 207] outputs the cache 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] will send the request 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 = 74102011-07-30 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.

Summary:This article briefly introduces the IOS memory cache mechanism. The next article will focus on the local cache mechanism.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.