iOS Cache to memory

Source: Internet
Author: User

The previous article describes how to upload and download files, and this article describes how to cache on iOS devices.

This article will only cover caching content in memory, and the next article describes caching content on iOS disks.

The purpose of caching is to use applications that can respond more quickly to user input, which is a program that runs efficiently. Sometimes we need to cache the data obtained by the remote Web server and reduce multiple requests to the same URL.

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, let's look at its cachepolicy type first.

1. Nsurlrequestuseprotocolcachepolicy nsurlrequest The default cache policy, using the Protocol protocol definition.
2, Nsurlrequestreloadignoringcachedata ignore the cache directly from the original address download.
3. Nsurlrequestreturncachedataelseload is downloaded from the original address only if no data exists in the cache.
4, Nsurlrequestreturncachedatadontload only use the cache data, if the cache does not exist, the request failed, for not establishing a network connection offline mode;
5, Nsurlrequestreloadignoringlocalandremotecachedata: Ignore the local and remote cache data, directly from the original address download, Similar to Nsurlrequestreloadignoringcachedata.
6NSURLRequestReloadRevalidatingCacheData: Verify that local data is the same as remote data, and download remote data if different, otherwise use local data.

Nsurlcache also provides a number of ways to facilitate our application caching mechanisms. Let me illustrate with an example that this example reduces our requests to the same URL multiple times. Look at the following code:

[Plain]View Plaincopy
  1. -(Ibaction) ButtonPress: (ID) sender
  2. {
  3. NSString *paramurlasstring= @ "http://www.baidu.com/";
  4. if ([paramurlasstring length] = = 0) {
  5. NSLog (@ "Nil or empty URL is given");
  6. Return
  7. }
  8. Nsurlcache *urlcache = [Nsurlcache Sharedurlcache];
  9. /* Set the cache size to 1m*/
  10. [Urlcache setmemorycapacity:1*1024*1024];
  11. Create a Nsurl
  12. Nsurl *url = [Nsurl urlwithstring:paramurlasstring];
  13. Create a request
  14. Nsmutableurlrequest *request =
  15. [Nsmutableurlrequest
  16. Requestwithurl:url
  17. Cachepolicy:nsurlrequestuseprotocolcachepolicy
  18. TIMEOUTINTERVAL:60.0F];
  19. Get the cache output from the request
  20. Nscachedurlresponse *response =
  21. [Urlcache Cachedresponseforrequest:request];
  22. Determine if there is a cache
  23. if (response! = nil) {
  24. NSLog (@ "If there is cache output, fetch data from cache");
  25. [Request Setcachepolicy:nsurlrequestreturncachedatadontload];
  26. }
  27. Self.connection = nil;
  28. /* Create nsurlconnection*/
  29. Nsurlconnection *newconnection =
  30. [[Nsurlconnection alloc] Initwithrequest:request
  31. Delegate:self
  32. Startimmediately:yes];
  33. Self.connection = newconnection;
  34. [Newconnection release];
  35. }

In this example, we request a URL for the www.baidu.com 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 code below, I will print out the requested process:

[Plain]View Plaincopy
  1. -(void) connection: (Nsurlconnection *) connection
  2. Didreceiveresponse: (Nsurlresponse *) response{
  3. NSLog (@ "will receive output");
  4. }
  5. -(Nsurlrequest *) connection: (Nsurlconnection *) connection
  6. Willsendrequest: (nsurlrequest *) request
  7. Redirectresponse: (Nsurlresponse *) redirectresponse{
  8. NSLog (@ "outgoing request");
  9. return (request);
  10. }
  11. -(void) connection: (Nsurlconnection *) connection
  12. Didreceivedata: (NSData *) data{
  13. NSLog (@ "Accept data");
  14. NSLog (@ "Data length =%lu", (unsigned long) [date]);
  15. }
  16. -(Nscachedurlresponse *) connection: (Nsurlconnection *) connection
  17. Willcacheresponse: (Nscachedurlresponse *) cachedresponse{
  18. NSLog (@ "will cache output");
  19. return (Cachedresponse);
  20. }
  21. -(void) connectiondidfinishloading: (nsurlconnection *) connection{
  22. NSLog (@ "request Complete");
  23. }
  24. -(void) connection: (Nsurlconnection *) connection
  25. Didfailwitherror: (Nserror *) error{
  26. NSLog (@ "request failed");
  27. }


When we first clicked on the button on the interface, the result was printed as follows:

[HTML]View Plaincopy
  1. 2011-07-30 18:50:24.910 caching[3971:207] Send request soon
  2. 2011-07-30 18:50:28.557 caching[3971:207] will receive output
  3. 2011-07-30 18:50:31.677 caching[3971:207] Accept data
  4. 2011-07-30 18:50:31.681 caching[3971:207] Data length = 4414
  5. 2011-07-30 18:50:31.682 caching[3971:207] Accept data
  6. 2011-07-30 18:50:31.682 caching[3971:207] Data length = 2996
  7. 2011-07-30 18:50:38.107 caching[3971:207] will cache output
  8. 2011-07-30 18:50:38.109 caching[3971:207] Request complete


After looking at our second click on the button on the interface, the print results are as follows:

2011-07-30 18:52:18.894 Caching[3971:207] 即将发送请求

2011-07-30 18:52:18.895 Caching[3971:207] 将接收输出
2011-07-30 18:52:18.895 Caching[3971:207] 接受数据
2011-07-30 18:52:18.896 Caching[3971:207] 数据长度为 = 7410
2011-07-30 18:52:18.896 Caching[3971:207] 请求完成

We see that there is no "cache output", and the requested data is the accumulation of the first request, that is, the second time the data is fetched from memory.

Summary: This article briefly describes the iOS memory caching mechanism, the next article will focus on the local caching mechanism

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.