Merge branch 'feature/small-download-cache'

This commit is contained in:
Brent Simmons committed 2026-05-15 13:55:21 -07:00
commit a27d3034e6
2 files changed
+1 -21

No files matched your search

@@ -22,7 +22,7 @@ struct DownloadCacheRecord: CacheRecord, Sendable {
nonisolated final class DownloadCache: Sendable {
static let shared = DownloadCache()
private let cache = Cache<DownloadCacheRecord>(timeToLive: 60 * 13, timeBetweenCleanups: 60 * 2)
private let cache = Cache<DownloadCacheRecord>(timeToLive: 60 * 3, timeBetweenCleanups: 60)
init() {
NotificationCenter.default.addObserver(self, selector: #selector(handleAppDidGoToBackground(_:)), name: .appDidGoToBackground, object: nil)
@@ -41,7 +41,6 @@ struct HTTP4xxResponse {
private let delegate: DownloadSessionDelegate
private var redirectCache = [URL: URL]()
private var queue = [URL]()
private let cache = DownloadCache.shared
public var progressInfo = ProgressInfo() {
didSet {
@@ -137,11 +136,6 @@ extension DownloadSession: @preconcurrency URLSessionTaskDelegate {
return
}
if let url = task.originalRequest?.url, error == nil {
Self.logger.debug("DownloadSession: Caching response for \(url)")
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?)
}
}
@@ -184,13 +178,6 @@ extension DownloadSession: @preconcurrency URLSessionDataDelegate {
let statusCode = response.forcedStatusCode
if statusCode >= 400 {
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)
}
}
Self.logger.debug("DownloadSession: canceling task due to >= 400 response \(response)")
if let url = taskInfo?.url {
@@ -252,13 +239,6 @@ private extension DownloadSession {
return
}
// 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: nil)
return
}
let urlRequest: URLRequest = {
var request = URLRequest(url: urlToUse)
if let conditionalGetInfo = delegate.downloadSession(self, conditionalGetInfoFor: url) {