From 63b3078231c64eeb674ba67391af0adf4e77e92c Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 30 Oct 2026 18:09:00 -0700 Subject: [PATCH] Figure out which images to remove in background. --- .../Sources/Images/DownloadedImageCache.swift | 4 ++-- .../DownloadedImageCacheTests.swift | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Modules/Images/Sources/Images/DownloadedImageCache.swift b/Modules/Images/Sources/Images/DownloadedImageCache.swift index 56022a98f..894cf8744 100644 --- a/Modules/Images/Sources/Images/DownloadedImageCache.swift +++ b/Modules/Images/Sources/Images/DownloadedImageCache.swift @@ -407,7 +407,7 @@ private extension DownloadedImageCache { let records = await ImageDatabase.shared.fetchDownloadedImageUsageByLeastRecentAccess() let fileDates = await Self.fileModificationDates(diskCache) - let removal = Self.imageRemoval(records: records, fileDates: fileDates, urlsInUse: Set(tasks.keys), now: startDate, limit: Self.limit, hardLimit: Self.hardLimit) + let removal = await Self.imageRemoval(records: records, fileDates: fileDates, urlsInUse: Set(tasks.keys), now: startDate, limit: Self.limit, hardLimit: Self.hardLimit) totalByteCount = removal.remainingByteCount removalByteCount = max(Self.limit.maximumByteCount, removal.remainingByteCount + Self.byteCountBetweenPasses) @@ -509,7 +509,7 @@ extension DownloadedImageCache { /// are removed, and when still over `hardLimit`, the least recently accessed of the rest. /// Nothing changed in the last minute is removed, since it may be in the middle of being saved. /// Images in `urlsInUse` are being read or downloaded, so they’re kept too. - nonisolated static func imageRemoval(records: [DownloadedImageUsage], fileDates: [String: Date], urlsInUse: Set, now: Date, limit: ByteLimit, hardLimit: ByteLimit) -> ImageRemoval { + @concurrent static func imageRemoval(records: [DownloadedImageUsage], fileDates: [String: Date], urlsInUse: Set, now: Date, limit: ByteLimit, hardLimit: ByteLimit) async -> ImageRemoval { let recentChangeDate = now.addingTimeInterval(-recentChangeInterval) let recentUseDate = now.addingTimeInterval(-recentUseInterval) diff --git a/Modules/Images/Tests/ImagesTests/DownloadedImageCacheTests.swift b/Modules/Images/Tests/ImagesTests/DownloadedImageCacheTests.swift index c0a248926..c2e2264f6 100644 --- a/Modules/Images/Tests/ImagesTests/DownloadedImageCacheTests.swift +++ b/Modules/Images/Tests/ImagesTests/DownloadedImageCacheTests.swift @@ -11,7 +11,7 @@ import Testing @Suite struct DownloadedImageCacheTests { - @Test func leastRecentlyUsedImagesAreRemovedDownToTarget() { + @Test func leastRecentlyUsedImagesAreRemovedDownToTarget() async { let now = Date() let longAgo = now.addingTimeInterval(-TimeInterval(days: 10)) let urls = ["https://example.com/a.jpg", "https://example.com/b.jpg", "https://example.com/c.jpg", "https://example.com/just-saved.jpg"] @@ -27,26 +27,26 @@ import Testing fileDates["unrecorded"] = longAgo fileDates["unrecordedJustSaved"] = now - let removal = DownloadedImageCache.imageRemoval(records: records, fileDates: fileDates, urlsInUse: [], now: now, limit: .init(maximumByteCount: 350), hardLimit: .init(maximumByteCount: 1000)) + let removal = await DownloadedImageCache.imageRemoval(records: records, fileDates: fileDates, urlsInUse: [], now: now, limit: .init(maximumByteCount: 350), hardLimit: .init(maximumByteCount: 1000)) #expect(removal.urls == ["https://example.com/no-file.jpg", urls[0], urls[1]]) #expect(removal.keys == ["unrecorded", keys[0], keys[1]]) #expect(removal.remainingByteCount == 200) } - @Test func imagesInUseAreKept() { + @Test func imagesInUseAreKept() async { let now = Date() let longAgo = now.addingTimeInterval(-TimeInterval(days: 10)) let records = (0..<3).map { DownloadedImageUsage(url: "https://example.com/\($0).jpg", byteCount: 100, lastAccessed: longAgo) } let fileDates = Dictionary(uniqueKeysWithValues: records.map { (DownloadedImageCache.diskKey($0.url), longAgo) }) - let removal = DownloadedImageCache.imageRemoval(records: records, fileDates: fileDates, urlsInUse: [records[0].url], now: now, limit: .init(maximumByteCount: 200), hardLimit: .init(maximumByteCount: 1000)) + let removal = await DownloadedImageCache.imageRemoval(records: records, fileDates: fileDates, urlsInUse: [records[0].url], now: now, limit: .init(maximumByteCount: 200), hardLimit: .init(maximumByteCount: 1000)) #expect(removal.urls == [records[1].url, records[2].url]) #expect(removal.remainingByteCount == 100) } - @Test func derivedCopiesAreKeptAndRemovedWithTheirImages() { + @Test func derivedCopiesAreKeptAndRemovedWithTheirImages() async { let now = Date() let longAgo = now.addingTimeInterval(-TimeInterval(days: 10)) let records = (0..<2).map { DownloadedImageUsage(url: "https://example.com/\($0).png", byteCount: 100, lastAccessed: longAgo.addingTimeInterval(TimeInterval($0))) } @@ -58,7 +58,7 @@ import Testing fileDates[key] = longAgo } - let removal = DownloadedImageCache.imageRemoval(records: records, fileDates: fileDates, urlsInUse: [], now: now, limit: .init(maximumByteCount: 150), hardLimit: .init(maximumByteCount: 1000)) + let removal = await DownloadedImageCache.imageRemoval(records: records, fileDates: fileDates, urlsInUse: [], now: now, limit: .init(maximumByteCount: 150), hardLimit: .init(maximumByteCount: 1000)) #expect(removal.urls == [records[0].url]) #expect(removal.keys == [DownloadedImageCache.diskKey(records[0].url), removedCopy, orphanedCopy]) @@ -73,17 +73,17 @@ import Testing #expect(interval > TimeInterval(days: 3) * 0.8 - 60 && interval < TimeInterval(days: 3) * 1.2) } - @Test func recentlyUsedImagesAreKeptUnlessOverHardLimit() { + @Test func recentlyUsedImagesAreKeptUnlessOverHardLimit() async { let now = Date() let records = (0..<5).map { DownloadedImageUsage(url: "https://example.com/\($0).jpg", byteCount: 100, lastAccessed: now.addingTimeInterval(-TimeInterval(days: 1) + TimeInterval($0))) } let keys = records.map { DownloadedImageCache.diskKey($0.url) } let fileDates = Dictionary(uniqueKeysWithValues: keys.map { ($0, now.addingTimeInterval(-TimeInterval(days: 1))) }) - let underHardLimit = DownloadedImageCache.imageRemoval(records: records, fileDates: fileDates, urlsInUse: [], now: now, limit: .init(maximumByteCount: 200), hardLimit: .init(maximumByteCount: 1000)) + let underHardLimit = await DownloadedImageCache.imageRemoval(records: records, fileDates: fileDates, urlsInUse: [], now: now, limit: .init(maximumByteCount: 200), hardLimit: .init(maximumByteCount: 1000)) #expect(underHardLimit.urls.isEmpty) #expect(underHardLimit.remainingByteCount == 500) - let overHardLimit = DownloadedImageCache.imageRemoval(records: records, fileDates: fileDates, urlsInUse: [], now: now, limit: .init(maximumByteCount: 200), hardLimit: .init(maximumByteCount: 400)) + let overHardLimit = await DownloadedImageCache.imageRemoval(records: records, fileDates: fileDates, urlsInUse: [], now: now, limit: .init(maximumByteCount: 200), hardLimit: .init(maximumByteCount: 400)) #expect(overHardLimit.urls == [records[0].url, records[1].url]) #expect(overHardLimit.remainingByteCount == 300) }