mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-30 17:29:23 +00:00
Use shared DownloadCache.
This commit is contained in:
1 parent
0b215c3c76
commit
bbc0313e79
2 files changed
+17
-58
No files matched your search
@@ -31,19 +31,6 @@ struct HTTP4xxResponse {
|
||||
}
|
||||
}
|
||||
|
||||
struct DownloadCacheRecord: CacheRecord {
|
||||
let dateCreated = Date()
|
||||
let data: Data
|
||||
let response: URLResponse?
|
||||
let error: NSError?
|
||||
|
||||
init(data: Data, response: URLResponse?, error: NSError?) {
|
||||
self.data = data
|
||||
self.response = response
|
||||
self.error = error
|
||||
}
|
||||
}
|
||||
|
||||
@objc public final class DownloadSession: NSObject {
|
||||
|
||||
public let downloadProgress = DownloadProgress(numberOfTasks: 0)
|
||||
@@ -56,12 +43,7 @@ struct DownloadCacheRecord: CacheRecord {
|
||||
private let delegate: DownloadSessionDelegate
|
||||
private var redirectCache = [URL: URL]()
|
||||
private var queue = [URL]()
|
||||
|
||||
// Static so that it can be shared by all instances of DownloadSession.
|
||||
// A feed might appear in both an On My Mac and iCloud account, for instance.
|
||||
// Cache items live for 13 minutes, in order to ensure no feed is downloaded more often
|
||||
// than 10 minutes (plus a little extra).
|
||||
private static let responseCache = Cache<DownloadCacheRecord>(timeToLive: 13 * 60, timeBetweenCleanups: 4 * 60)
|
||||
private let cache = DownloadCache.shared
|
||||
|
||||
// 429 Too Many Requests responses
|
||||
private var retryAfterMessages = [String: HTTPResponse429]()
|
||||
@@ -143,7 +125,7 @@ extension DownloadSession: URLSessionTaskDelegate {
|
||||
if let url = task.originalRequest?.url {
|
||||
Self.logger.debug("DownloadSession: Caching response for \(url)")
|
||||
let responseToCache = DownloadCacheRecord(data: info.data as Data, response: info.urlResponse, error: error as NSError?)
|
||||
Self.responseCache[url.absoluteString] = responseToCache
|
||||
cache[url.absoluteString] = responseToCache
|
||||
}
|
||||
|
||||
delegate.downloadSession(self, downloadDidComplete: info.url, response: info.urlResponse, data: info.data as Data, error: error as NSError?)
|
||||
@@ -192,8 +174,7 @@ extension DownloadSession: URLSessionDataDelegate {
|
||||
if statusCode != HTTPResponseCode.tooManyRequests {
|
||||
if let urlString = response.url?.absoluteString {
|
||||
Self.logger.debug("DownloadSession: Caching >= 400 response for \(urlString)")
|
||||
let responseToCache = DownloadCacheRecord(data: Data(), response: response, error: nil)
|
||||
Self.responseCache[urlString] = responseToCache
|
||||
cache.add(urlString, data: nil, response: response, error: nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,9 +234,9 @@ private extension DownloadSession {
|
||||
}
|
||||
|
||||
// Check cache
|
||||
if let cachedResponse = Self.responseCache[urlToUse.absoluteString] {
|
||||
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, error: cachedResponse.error)
|
||||
delegate.downloadSession(self, downloadDidComplete: url, response: cachedResponse.response, data: cachedResponse.data ?? Data(), error: cachedResponse.error)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -20,12 +20,9 @@ public typealias DownloadCallback = @MainActor (Data?, URLResponse?, Error?) ->
|
||||
public static let shared = Downloader()
|
||||
private let urlSession: URLSession
|
||||
private var callbacks = [URL: [DownloadCallback]]()
|
||||
|
||||
// Cache — short-lived
|
||||
private let cache = Cache<DownloaderRecord>(timeToLive: 60 * 3, timeBetweenCleanups: 60 * 2)
|
||||
|
||||
private let cache = DownloadCache.shared
|
||||
|
||||
nonisolated private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "Downloader")
|
||||
nonisolated private static let debugLoggingEnabled = false
|
||||
|
||||
private init() {
|
||||
let sessionConfiguration = URLSessionConfiguration.ephemeral
|
||||
@@ -64,9 +61,7 @@ public typealias DownloadCallback = @MainActor (Data?, URLResponse?, Error?) ->
|
||||
// Return cached record if available.
|
||||
if isCacheableRequest {
|
||||
if let cachedRecord = cache[url.absoluteString] {
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("Downloader: returning cached record for \(url)")
|
||||
}
|
||||
Self.logger.debug("Downloader: returning cached record for \(url)")
|
||||
callback(cachedRecord.data, cachedRecord.response, cachedRecord.error)
|
||||
return
|
||||
}
|
||||
@@ -74,16 +69,12 @@ public typealias DownloadCallback = @MainActor (Data?, URLResponse?, Error?) ->
|
||||
|
||||
// Add callback. If there is already a download in progress for this URL, return early.
|
||||
if callbacks[url] == nil {
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("Downloader: downloading \(url)")
|
||||
}
|
||||
Self.logger.debug("Downloader: downloading \(url)")
|
||||
callbacks[url] = [callback]
|
||||
} else {
|
||||
// A download is already be in progress for this URL. Don’t start a separate download.
|
||||
// Add the callback to the callbacks array for this URL.
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("Downloader: download in progress for \(url) — adding callback")
|
||||
}
|
||||
Self.logger.debug("Downloader: download in progress for \(url) — adding callback")
|
||||
callbacks[url]?.append(callback)
|
||||
return
|
||||
}
|
||||
@@ -94,11 +85,8 @@ public typealias DownloadCallback = @MainActor (Data?, URLResponse?, Error?) ->
|
||||
let task = urlSession.dataTask(with: urlRequestToUse) { (data, response, error) in
|
||||
|
||||
if isCacheableRequest {
|
||||
if Self.debugLoggingEnabled {
|
||||
Self.logger.debug("Downloader: caching record for \(url)")
|
||||
}
|
||||
let cachedRecord = DownloaderRecord(data: data, response: response, error: error)
|
||||
self.cache[url.absoluteString] = cachedRecord
|
||||
Self.logger.debug("Downloader: caching response for \(url)")
|
||||
self.cache.add(url.absoluteString, data: data, response: response, error: error)
|
||||
}
|
||||
|
||||
Task { @MainActor in
|
||||
@@ -124,13 +112,11 @@ private extension Downloader {
|
||||
return
|
||||
}
|
||||
|
||||
if Self.debugLoggingEnabled {
|
||||
let count = callbacksForURL.count
|
||||
if count == 1 {
|
||||
Self.logger.debug("Downloader: calling 1 callback for URL \(url)")
|
||||
} else {
|
||||
Self.logger.debug("Downloader: calling \(count) callbacks for URL \(url)")
|
||||
}
|
||||
let count = callbacksForURL.count
|
||||
if count == 1 {
|
||||
Self.logger.debug("Downloader: calling 1 callback for URL \(url)")
|
||||
} else {
|
||||
Self.logger.debug("Downloader: calling \(count) callbacks for URL \(url)")
|
||||
}
|
||||
|
||||
for callback in callbacksForURL {
|
||||
@@ -138,11 +124,3 @@ private extension Downloader {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DownloaderRecord: CacheRecord, Sendable {
|
||||
|
||||
let dateCreated = Date()
|
||||
let data: Data?
|
||||
let response: URLResponse?
|
||||
let error: Error?
|
||||
}
|
||||
Reference in new issue
Block a user