title: Large image URL response not cached by URLSession: Why?
body: <p>I use a <code>URLSession</code> data task to download several JPG images from a backend. As the images are rather large in size (~500 KB) I want to cache the respective responses until they have expired (i.e. they have exceeded their max-age).</p>
  
  <p>This is the code I use for downloading the images:</p>
  
  <pre><code>let request = URLRequest(url: url, 
                            cachePolicy: .useProtocolCachePolicy, 
                            timeoutInterval: 10.0)
  
  let task = URLSession.shared.dataTask(with: request) { (data, _, error) in
  
      // Error:
      guard let imageData = data, error == nil, let image = UIImage(data: imageData) else {
          DispatchQueue.main.async {
              completion(nil)
          }
          return
      }
  
      // Success:
      DispatchQueue.main.async {
          completion(image)
      }
  }
  
  task.resume()
  </code></pre>
  
  <p>Curiously, this works great with caching for all images except for one. For some reason, this particular image is <em>always</em> downloaded again – <strong>its response is not cached</strong>.</p>
  
  <p>The only difference between the responses that I can spot is that the image whose corresponding response is not cached has the biggest file size. While all other images are &lt; 500 kB, this particular image is slightly > 500 kB. </p>
  
  <p>I've played around with the shared cache size and set it to a ridiculously high value, with no effect:</p>
  
  <pre><code>URLCache.shared = URLCache(memoryCapacity: 1000 * 1024 * 1024, 
                             diskCapacity:   1000 * 1024 * 1024, 
                             diskPath:       nil)
  </code></pre>
  
  <p>I've checked that the <em>Cache-Control</em> header field is correctly set in the response:</p>
  
  <pre><code>Cache-Control: public, max-age=86400
  </code></pre>
  
  <p>and the Age header field is always below <code>max-age</code>, for example:</p>
  
  <pre><code>Age: 3526
  </code></pre>
  
  <h3>What could be the reason for a single response not to be cached?</h3>
  
  <p>How can I fix this?</p>