mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-03 18:56:14 +00:00
Prevent images being read or being downloaded from being removed by a removal pass.
This commit is contained in:
1 parent
e08a815626
commit
1feafcf4f1
2 files changed
+19
-6
No files matched your search
@@ -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<String>, 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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user