title: Implementing NSURLRequest, NSURLConnection and NSURLRequestReturnCacheDataElseLoad policy
body: <p>Do anyone have a sample code for implementing image caching using NSURLRequest, NSURLConnection and NSURLRequestReturnCacheDataElseLoad policy?</p>
  
  <p>I am using the following code but seems like no caching is happening. All the time I am getting the image from the URL. Please tell me what's wrong here:</p>
  
  <pre><code>NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://i54.tinypic.com/10pd2jk.png"]];
      NSData *data=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
      NSLog(@"Received %d bytes", [data length]);
      [req setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
      data=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
      NSLog(@"Received %d bytes", [data length]);
      [[NSURLCache sharedURLCache] setMemoryCapacity:1024*1024*10];
  
  
      UIImage *myImage = [UIImage imageWithData:data];
      UIImageView *sd = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
      sd.image = myImage;
      [self.view addSubview:sd];
  </code></pre>