diff --git a/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift b/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift index 5d353cbb3..da360dc1f 100644 --- a/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift +++ b/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift @@ -95,6 +95,7 @@ extension Notification.Name { /// `.articleThumbnailDidBecomeAvailable` once the image is ready. public func thumbnail(url: String, mode: Mode, articleTitle: String?) -> RSImage? { if let image = imageCache.object(forKey: Self.cacheKey(url, mode)) { + DownloadedImageCache.shared.imageWasUsed(url, checksForUpdates: false) return image } findImage(url, mode: mode, downloadPriority: .visible, activityDetail: articleTitle) diff --git a/Modules/Images/Sources/Images/DownloadedImageCache.swift b/Modules/Images/Sources/Images/DownloadedImageCache.swift index 2d710c1e8..d72de0a69 100644 --- a/Modules/Images/Sources/Images/DownloadedImageCache.swift +++ b/Modules/Images/Sources/Images/DownloadedImageCache.swift @@ -126,6 +126,24 @@ extension Notification.Name { downloadLimiter.prioritize(key: url) } + /// At most once a day per image: saves the access date, which orders images for removal, + /// and, when `checksForUpdates`, checks with the server if the copy is due. + /// Callers that keep images in memory call this on memory hits too, so images in use aren’t removed first. + func imageWasUsed(_ url: String, checksForUpdates: Bool) { + let now = Date() + if let readDate = readDatesNoted[url], now.timeIntervalSince(readDate) < Self.readNoteInterval { + return + } + readDatesNoted[url] = now + ImageDatabase.shared.saveDownloadedImageAccessDate(url: url) + guard checksForUpdates else { + return + } + Task { + await revalidateIfDue(url) + } + } + /// Failures while offline say nothing about the servers, so retries can start right away. @objc func handleNetworkStatusDidChange(_ notification: Notification) { retryDelays.removeAll() @@ -137,7 +155,7 @@ private extension DownloadedImageCache { func findImage(_ url: String, downloadPriority: DownloadPriority?, checksForUpdates: Bool, activityOwner: ActivityOwner?, activityKind: ActivityKind?, activityDetail: String?) async -> Result { if let data = await Self.readFromDisk(diskCache, key: Self.diskKey(url)) { - imageWasRead(url, checksForUpdates: checksForUpdates) + imageWasUsed(url, checksForUpdates: checksForUpdates) return .success(data) } @@ -292,23 +310,6 @@ private extension DownloadedImageCache { return true } - /// At most once a day per image: saves the access date, which orders images for removal, - /// and, when `checksForUpdates`, checks with the server if the copy is due. - func imageWasRead(_ url: String, checksForUpdates: Bool) { - let now = Date() - if let readDate = readDatesNoted[url], now.timeIntervalSince(readDate) < Self.readNoteInterval { - return - } - readDatesNoted[url] = now - ImageDatabase.shared.saveDownloadedImageAccessDate(url: url) - guard checksForUpdates else { - return - } - Task { - await revalidateIfDue(url) - } - } - // MARK: - Revalidating Images /// Doesn’t use the download limiter, since icons aren’t limited either. diff --git a/Modules/Images/Sources/Images/ImageDownloader.swift b/Modules/Images/Sources/Images/ImageDownloader.swift index 5dad1b9e5..9f613cfb8 100644 --- a/Modules/Images/Sources/Images/ImageDownloader.swift +++ b/Modules/Images/Sources/Images/ImageDownloader.swift @@ -102,6 +102,7 @@ extension ImageDownloadError: LocalizedError { return nil } if let data = imageCache.object(forKey: url as NSString) { + DownloadedImageCache.shared.imageWasUsed(url, checksForUpdates: true) return data as Data } if ImageDatabase.shared.recentlyFailed(url: url) {