diff --git a/Modules/Images/Sources/Images/DownloadedImageCache.swift b/Modules/Images/Sources/Images/DownloadedImageCache.swift index e8acb0655..af2dae9ad 100644 --- a/Modules/Images/Sources/Images/DownloadedImageCache.swift +++ b/Modules/Images/Sources/Images/DownloadedImageCache.swift @@ -267,7 +267,7 @@ private extension DownloadedImageCache { let records = await ImageDatabase.shared.fetchDownloadedImageUsageByLeastRecentAccess() let fileDates = await Self.fileModificationDates(diskCache) - let removal = Self.imageRemoval(records: records, fileDates: fileDates, now: startDate, limit: Self.limit, hardLimit: Self.hardLimit) + let removal = 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) @@ -355,7 +355,8 @@ extension DownloadedImageCache { /// are removed. Then, when over `limit`, the least recently accessed images not used recently /// 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. - nonisolated static func imageRemoval(records: [DownloadedImageUsage], fileDates: [String: Date], now: Date, limit: ByteLimit, hardLimit: ByteLimit) -> ImageRemoval { + /// 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 { let recentChangeDate = now.addingTimeInterval(-recentChangeInterval) let recentUseDate = now.addingTimeInterval(-recentUseInterval) @@ -382,7 +383,7 @@ extension DownloadedImageCache { if byteCount <= limit.byteCountAfterRemoval || record.lastAccessed >= date { break } - if keys.contains(key) { + if keys.contains(key) || urlsInUse.contains(record.url) { continue } urls.append(record.url) diff --git a/Modules/Images/Tests/ImagesTests/DownloadedImageCacheTests.swift b/Modules/Images/Tests/ImagesTests/DownloadedImageCacheTests.swift index f2764d51e..7bcaca3ee 100644 --- a/Modules/Images/Tests/ImagesTests/DownloadedImageCacheTests.swift +++ b/Modules/Images/Tests/ImagesTests/DownloadedImageCacheTests.swift @@ -27,13 +27,25 @@ import Testing fileDates["unrecorded"] = longAgo fileDates["unrecorded-just-saved"] = now - let removal = DownloadedImageCache.imageRemoval(records: records, fileDates: fileDates, now: now, limit: .init(maximumByteCount: 350), hardLimit: .init(maximumByteCount: 1000)) + let removal = 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() { + 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)) + + #expect(removal.urls == [records[1].url, records[2].url]) + #expect(removal.remainingByteCount == 100) + } + @Test func shortMaxAgeIsRaisedToThreeDays() throws { let url = try #require(URL(string: "https://example.com/icon.png")) let httpResponse = try #require(HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: ["Cache-Control": "max-age=60", "ETag": "\"abc\""])) @@ -48,11 +60,11 @@ import Testing 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, now: now, limit: .init(maximumByteCount: 200), hardLimit: .init(maximumByteCount: 1000)) + let underHardLimit = 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, now: now, limit: .init(maximumByteCount: 200), hardLimit: .init(maximumByteCount: 400)) + let overHardLimit = 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) }