使用storeCachedResponse

时间:2018-10-22 21:31:07

标签: ios http caching nsurlsession nsurlcache

目标

我正在尝试将URLRequest中的数据/响应注入到我的缓存中的另一个 URLRequest中。

设置

这只是示例代码。准备将其转储到项目中。

我想做的是使用从我的landscapeURLString网络请求中检索到的响应和数据...存储到我的lizardURLString 请求的会话缓存中。

import UIKit

class ViewController: UIViewController {

    lazy var defaultSession : URLSession = {
        let urlCache = URLCache(memoryCapacity: 500 * 1024 * 1024, diskCapacity: 500 * 1024 * 1024, diskPath: "something")
        let configuration = URLSessionConfiguration.default
        configuration.urlCache = urlCache
        let session = URLSession(configuration: configuration)

        return session
    }()
    lazy var downloadLizzardbutton : UIButton = {
        let btn = UIButton()
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.setTitle("download lizard image OFFLINE", for: .normal)
        btn.backgroundColor = .blue
        btn.addTarget(self, action: #selector(downloadLizardAction), for: .touchUpInside)
        return btn
    }()

    let imageView : UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()

    // I make sure my internet is set to OFF so that it forces this to be read from cache...
    @objc func downloadLizardAction() {
        downloadImage(from: lizardURLString, from: defaultSession)
    }
    let lizardURLString = "https://upload.wikimedia.org/wikipedia/commons/e/e0/Large_Scaled_Forest_Lizard.jpg"
    let landscapeURLString = "https://images.pexels.com/photos/414171/pexels-photo-414171.jpeg"        

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(imageView)
        view.addSubview(downloadLizzardbutton)
        imageView.pinToAllEdges(of: view)

        downloadImage(from: landscapeURLString, from: defaultSession)
    }
    private func downloadImage(from urlString: String, from session : URLSession){
        guard let url = URL(string: urlString) else{
            fatalError("bad String we got!")
        }

        let urlRequest = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 15)
        print("url.hashValue: \(urlRequest.hashValue)")

        let task = session.dataTask(with: urlRequest) { [weak self] (data, response, error) in

            guard error == nil else {
                print(error)
                return
            }
            guard let httpResponse = response as? HTTPURLResponse,
                (200...299).contains(httpResponse.statusCode) else {
                    print("response NOT 2xx: \(response)")
                    return
            }

            for header in httpResponse.allHeaderFields{
                if let key = header.key as? String, key == "Cache-Control"{
                    print("found Cache-Control: \(httpResponse.allHeaderFields["Cache-Control"])")
                }
            }

            if let data = data,
                let image = UIImage(data: data){
                let lizardURL = URL(string: self!.lizardURLString)
                let lizardURLRequest = URLRequest(url: lizardURL!)

                let landscapeCachedURLPResponse : CachedURLResponse = CachedURLResponse(response: response!, data: data, userInfo:nil, storagePolicy: .allowed)
                print("before storing into cache: \(String(describing: session.configuration.urlCache?.cachedResponse(for: lizardURLRequest)))")

                session.configuration.urlCache?.storeCachedResponse(landscapeCachedURLPResponse, for: lizardURLRequest)    

                print("after storing into cache: \(String(describing: session.configuration.urlCache?.cachedResponse(for: lizardURLRequest)))")
                print("lizardRequest.hashValue: \(lizardURLRequest.hashValue)")

                DispatchQueue.main.async {
                    self?.imageView.image = image
                }
            }
        }
        task.resume()
    }        
}


extension UIView{

    func pinToAllEdges(of view: UIView){
        let leading = leadingAnchor.constraint(equalTo: view.leadingAnchor)
        let top = topAnchor.constraint(equalTo: view.topAnchor)
        let trailing = trailingAnchor.constraint(equalTo: view.trailingAnchor)
        let bottom = bottomAnchor.constraint(equalTo: view.bottomAnchor)

        NSLayoutConstraint.activate([leading, top, trailing, bottom])
    }
}

我已经验证过的东西:

  • 我的landscapeURLString有一个cache-control标头,其中max-age31536000
  • 如果是全新安装,则之前存储到缓存中,我的cachedResponse for lizardURLString是nil。但是 存储后,它不再是nil。结果,我得出结论,我已经成功地将某些东西存储到缓存中了!
  • 我还怀疑URLCache将URLRequest视为键。因此,我打印了lizardURLString的hashValue。它与我存储的密钥相同。结合以上几点,我得出结论,确切的密钥存在于缓存中!
  • 我还看到,将其存储在缓存中时,currentMemoryUsage会增加。

我如何测试以及所见:

  1. 我只是下载风景图片。
  2. 关闭我的互联网
  3. 单击按钮以下载蜥蜴图像。

很显然它是离线的。我希望它可以从缓存中使用,但事实并非如此。我得到的只是一个超时!

我还尝试将cachePolicy更改为returnCacheDataElseLoad,但这也无济于事

EDIT1:

我还尝试了大卫所说的话:

let landscapeHTTPResponse : HTTPURLResponse = HTTPURLResponse(url: self!.lizardURL, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: (httpResponse.allHeaderFields as! [String : String]))!
let landscapedCachedURLPResponse : CachedURLResponse = CachedURLResponse(response: landscapeHTTPResponse, data: data, userInfo:nil, storagePolicy: .allowed)

,并将存储的landscapedCachedURLPResponse放入缓存。那也不起作用。它也会超时-并非每次都进入缓存。

EDIT2:

所以我取得了一些进展。或者,也许退后了一步,又前进了一步。

我试图查看是否可以为相同 URL存储响应,并查看在清空缓存后是否可以检索响应。我没能力

我正在创建这样的缓存响应:

let cachedResponse = CachedURLResponse(response: response!, data: data, userInfo:nil, storagePolicy: .allowed)

或类似这样:

let cachedResponse = CachedURLResponse(response: response!, data: data)

这部分起作用了吗?:

let cachedResponseFromCache = session.configuration.urlCache?.cachedResponse(for: self!.landscapeURLRequest)
self._cachedResponse = cachedResponseFromCache

然后我:

  1. 刷新了缓存
  2. 关闭互联网
  3. 试图下载图像,但没有成功,这是好的。这是预期的行为
  4. cachedResponseFromCache属性存储到缓存中。
  5. 能够从缓存中检索!

我不确定从缓存本身退出与从Response + Data创建缓存之间有什么区别。

这很重要,因为我开始质疑是否还有某种形式的internal bugs in URLCache。这使我有理由相信它可能可以按预期运行。

现在,我知道将数据存储到缓存的过程了。我知道我的URLResponse很好。我只需要通过映射URLRequest来工作

EDIT3:

Guy Kogus建议我的URL必须来自同一来源。 因此,一旦我下载了他提到的bearImage,我的lizardImage就会通过。瞧!

作为一个非常重要的调试注释,我了解到:即使您在问题的某个部分(成功地为其自身缓存了风景图像)上取得了成功,更改变量(此处更改初始URL)也总是可以更改整个测试结果。

他怀疑这是因为标题中的Server是共享的,这对于查找cachedResponse很重要。

我反驳了这一说法,说我的lizardURLRequest是在在线时发出的,因此没有什么可比的,但是它可以工作! 因此,下一个想法是,它可能与URL的某些部分有关,例如它的第一部分或其他内容。

因此,我去了以下网站更改了lizardURL:

https://upload.wikimedia.org/wikipedia/commons/e/e0/Large_Scaled_Forest_Lizard.jpg

类似于:     https://skdhfsupload.qwiklkjlkjimedia.com/qwikipehkjdia/eeeeeecommons/sdalfjkdse/aldskfjae0/extraParam/anotherextraparam/asasdLarge_Scaled_Forest_Lizard.jpeg

我在网址中添加了哑字符。我还添加了额外的细分。我在最后更改了文件类型。

仍然有效。因此,我唯一可以得出的结论是,标头中的某项内容正在做出决策。

我的landscapeURL的标头是:(为此,缓存另一个URL无效)

Content-Length : 997361
x-cache : HIT, MISS
cf-ray : 472793e93ce39574-IAD
x-served-by : cache-lax8621-LAX, cache-iad2132-IAD
cf-cache-status : HIT
Last-Modified : Sun, 14 Oct 2018 2:10:05 GMT
Accept-Ranges : bytes
Vary : Accept-Encoding
x-content-type-options : nosniff
Content-Type : image/jpeg
expect-ct : max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Set-Cookie : __cfduid=d5f5fd59ce5ff9ac86e42f8c008708ae61541004176; expires=Thu, 31-Oct-19 16:42:56 GMT; path=/; domain=.pexels.com; HttpOnly
Expires : Thu, 31 Oct 2019 16:42:56 GMT
Server : cloudflare
Cache-Control : public, max-age=31536000
Date : Wed, 31 Oct 2018 16:42:56 GMT

我的BearURL的标头是:(为此可以缓存另一个URL)

Date : Wed, 31 Oct 2018 16:46:38 GMT
Content-Length : 215104
x-client-ip : 2001:558:1400:4e:808c:2738:43e:36f5
access-control-expose-headers : Age, Date, Content-Length, Content-Range, X-Content-Duration, X-Cache, X-Varnish
x-cache : cp1076 miss, cp1088 hit/21
Age : 27646
Etag : 00e21950bf432476c91b811bb685b6af
Strict-Transport-Security : max-age=106384710; includeSubDomains; preload
x-analytics : https=1;nocookies=1
Accept-Ranges : bytes
x-object-meta-sha1base36 : 42tq5grg9rq1ydmqd4z5hmmqj6h2309
x-varnish : 48388488, 503119619 458396839
x-cache-status : hit-front
Content-Type : image/jpeg
x-trans-id : tx08ed43bbcc1946269a9a3-005bd97070
Last-Modified : Fri, 04 Oct 2013 23:30:08 GMT
Access-Control-Allow-Origin : *
timing-allow-origin : *
x-timestamp : 1380929407.39127
Via : 1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1)

重要提示:

对于BearURL,对BearURL lizardURL或任何其他URL进行缓存。 对于landscapeURL,缓存仅适用于landscapeURL本身。它不适用于任何 other 网址。

所以问题的当前状态是:要使其正常工作,需要包含哪些 headers

3 个答案:

答案 0 :(得分:3)

欢迎来到异步缓存的美好世界。 NSURLCache是​​高度异步的。仅仅因为您将数据塞入其中并不意味着它可用于检索。您必须先让主运行循环返回,然后才能使用它,甚至可能要稍等片刻。在存储响应后立即返回响应的失败并非罕见。尝试在大约五秒钟后分派它。

第二,您的缓存可能有点小,无法存储多兆字节的图像。尝试将其增大,看看是否有帮助。

最后,当您说“关闭互联网”时,您是什么意思?您说您超时了。通常,如果您在禁用所有连接的情况下将设备置于“飞行”模式,则在出现故障(指示没有连接)之前,设备不应坐在那里过多时间。如果这没有发生,则正在发生奇怪的事情,几乎就像在会话上设置了waitsForConnectivity一样。 (不是在后台发出网络请求,是吗?如果是,请尝试将waitsForConnectivity显式设置为NO,这样它们就不会等待连接可用。)

此外,对于这种用法,您可能必须去除Vary:Accept-Encoding标头或提供一致的用户代理字符串。该头导致高速缓存基本上是每个浏览器。这可能会导致缓存行为异常,并且可能是您所看到的怪异原因。

请注意,剥离Vary标头有点麻烦,并且可能不是解决此问题的最正确方法;理想情况下,您应该调整必须调整的所有传出标头字段,以便即使存在该标头也可以使用。但是您必须对其进行研究,并弄清楚需要哪些领域,因为我不知道。 :-)

答案 1 :(得分:2)

这不是一个完整的答案,但是它应该使您朝着正确的方向前进。

问题与您的代码无关,我认为这基本上没问题。问题在于您从landscapeURLString获得的响应,因为该图像存储在Cloudflare中。如果您使用2个来自同一来源的图片(例如,尝试使用this bear from wikipedia而不是来自images.pexels.com的图片),则它应该可以工作。

我尝试打印出下载images.pexels.com图片的响应和标题,这就是我看到的内容:

response: <NSHTTPURLResponse: 0x600002bf65c0> { URL: https://upload.wikimedia.org/wikipedia/commons/e/e0/Large_Scaled_Forest_Lizard.jpg } { Status Code: 200, Headers {
    "Accept-Ranges" =     (
        bytes
    );
    "Cache-Control" =     (
        "public, max-age=31536000"
    );
    "Content-Length" =     (
        997361
    );
    "Content-Type" =     (
        "image/jpeg"
    );
    Date =     (
        "Wed, 31 Oct 2018 11:38:52 GMT"
    );
    Expires =     (
        "Thu, 31 Oct 2019 11:38:52 GMT"
    );
    "Last-Modified" =     (
        "Fri, 26 Oct 2018 6:31:56 GMT"
    );
    Server =     (
        cloudflare
    );
    Vary =     (
        "Accept-Encoding"
    );
    "cf-cache-status" =     (
        HIT
    );
    "cf-ray" =     (
        "4725d67b0ae461bd-BCN"
    );
    "expect-ct" =     (
        "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""
    );
    "x-cache" =     (
        "HIT, MISS"
    );
    "x-content-type-options" =     (
        nosniff
    );
    "x-served-by" =     (
        "cache-lax8643-LAX, cache-mad9437-MAD"
    );
} }
headers: ["Accept-Ranges": "bytes", "Content-Type": "image/jpeg", "Last-Modified": "Fri, 26 Oct 2018 6:31:56 GMT", "Vary": "Accept-Encoding", "cf-ray": "4725d67b0ae461bd-BCN", "Date": "Wed, 31 Oct 2018 11:38:52 GMT", "Server": "cloudflare", "Expires": "Thu, 31 Oct 2019 11:38:52 GMT", "x-content-type-options": "nosniff", "expect-ct": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"", "x-cache": "HIT, MISS", "x-served-by": "cache-lax8643-LAX, cache-mad9437-MAD", "cf-cache-status": "HIT", "Content-Length": "997361", "Cache-Control": "public, max-age=31536000"]

其中可能存在某种试图将请求URL与导致缓存未命中的响应字段进行匹配的方法,但是我对此并不足够了解。有人可能会为您找到它(因此,为什么我说这个答案不完整)。

答案 2 :(得分:0)

我通过用以下命令替换dataTaskcompleteHandler中的第一个警卫来解决此问题:

guard error == nil else {
  print(error)

  if let cr = session.configuration.urlCache?.cachedResponse(for: urlRequest){
     let image = UIImage(data: cr.data)
     DispatchQueue.main.async {
       self?.imageView.image = image
     }
   }

   return
}

如果请求失败,它将对该请求进行缓存的响应