Skip caching errors — those are things like connectivity errors, and shouldn’t be cached.

This commit is contained in:
Brent Simmons committed 2025-10-17 22:39:12 -07:00
1 parent 4343afd27c
commit e064aea97d
3 files changed
+9 -12

No files matched your search

@@ -12,12 +12,10 @@ struct DownloadCacheRecord: CacheRecord {
let dateCreated = Date()
let data: Data?
let response: URLResponse?
let error: NSError?
init(data: Data?, response: URLResponse?, error: NSError?) {
init(data: Data?, response: URLResponse?) {
self.data = data
self.response = response
self.error = error
}
}
@@ -35,8 +33,8 @@ final class DownloadCache: Sendable {
}
}
func add(_ urlString: String, data: Data?, response: URLResponse?, error: Error?) {
let cacheRecord = DownloadCacheRecord(data: data, response: response, error: error as? NSError)
func add(_ urlString: String, data: Data?, response: URLResponse?) {
let cacheRecord = DownloadCacheRecord(data: data, response: response)
cache[urlString] = cacheRecord
}
}
@@ -122,10 +122,9 @@ extension DownloadSession: URLSessionTaskDelegate {
return
}
if let url = task.originalRequest?.url {
if let url = task.originalRequest?.url, error == nil {
Self.logger.debug("DownloadSession: Caching response for \(url)")
let responseToCache = DownloadCacheRecord(data: info.data as Data, response: info.urlResponse, error: error as NSError?)
cache[url.absoluteString] = responseToCache
cache.add(url.absoluteString, data: info.data as Data, response: info.urlResponse)
}
delegate.downloadSession(self, downloadDidComplete: info.url, response: info.urlResponse, data: info.data as Data, error: error as NSError?)
@@ -172,7 +171,7 @@ extension DownloadSession: URLSessionDataDelegate {
if statusCode != HTTPResponseCode.tooManyRequests {
if let urlString = response.url?.absoluteString {
Self.logger.debug("DownloadSession: Caching >= 400 response for \(urlString)")
cache.add(urlString, data: nil, response: response, error: nil)
cache.add(urlString, data: nil, response: response)
}
}
@@ -234,7 +233,7 @@ private extension DownloadSession {
// Check cache
if let cachedResponse = cache[urlToUse.absoluteString] {
Self.logger.info("DownloadSession: using cached response for \(urlToUse) - \(cachedResponse.response?.forcedStatusCode ?? -1)")
delegate.downloadSession(self, downloadDidComplete: url, response: cachedResponse.response, data: cachedResponse.data ?? Data(), error: cachedResponse.error)
delegate.downloadSession(self, downloadDidComplete: url, response: cachedResponse.response, data: cachedResponse.data ?? Data(), error: nil)
return
}
+2 -2
View File
@@ -62,7 +62,7 @@ public typealias DownloadCallback = @MainActor (Data?, URLResponse?, Error?) ->
if isCacheableRequest {
if let cachedRecord = cache[url.absoluteString] {
Self.logger.debug("Downloader: returning cached record for \(url)")
callback(cachedRecord.data, cachedRecord.response, cachedRecord.error)
callback(cachedRecord.data, cachedRecord.response, nil)
return
}
}
@@ -86,7 +86,7 @@ public typealias DownloadCallback = @MainActor (Data?, URLResponse?, Error?) ->
if isCacheableRequest {
Self.logger.debug("Downloader: caching response for \(url)")
self.cache.add(url.absoluteString, data: data, response: response, error: error)
self.cache.add(url.absoluteString, data: data, response: response)
}
Task { @MainActor in