diff --git a/Modules/RSWeb/Sources/RSWeb/DownloadCache.swift b/Modules/RSWeb/Sources/RSWeb/DownloadCache.swift index af70ea3a2..edbaaef4f 100644 --- a/Modules/RSWeb/Sources/RSWeb/DownloadCache.swift +++ b/Modules/RSWeb/Sources/RSWeb/DownloadCache.swift @@ -25,9 +25,14 @@ nonisolated final class DownloadCache: Sendable { private let cache = Cache(timeToLive: 60 * 13, timeBetweenCleanups: 60 * 2) init() { + NotificationCenter.default.addObserver(self, selector: #selector(handleAppDidGoToBackground(_:)), name: .appDidGoToBackground, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleLowMemory(_:)), name: .lowMemory, object: nil) } + @objc func handleAppDidGoToBackground(_ notification: Notification) { + cache.removeAll() + } + @objc func handleLowMemory(_ notification: Notification) { cache.removeAll() } diff --git a/Modules/RSWeb/Sources/RSWeb/HTMLMetadataDownloader.swift b/Modules/RSWeb/Sources/RSWeb/HTMLMetadataDownloader.swift index b264c09dd..50838f620 100644 --- a/Modules/RSWeb/Sources/RSWeb/HTMLMetadataDownloader.swift +++ b/Modules/RSWeb/Sources/RSWeb/HTMLMetadataDownloader.swift @@ -24,9 +24,14 @@ nonisolated public final class HTMLMetadataDownloader: Sendable { private let urlsReturning4xxsLock = OSAllocatedUnfairLock(initialState: Set()) init() { + NotificationCenter.default.addObserver(self, selector: #selector(handleAppDidGoToBackground(_:)), name: .appDidGoToBackground, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleLowMemory(_:)), name: .lowMemory, object: nil) } + @objc func handleAppDidGoToBackground(_ notification: Notification) { + cache.removeAll() + } + @objc func handleLowMemory(_ notification: Notification) { cache.removeAll() } diff --git a/Shared/Extensions/ArticleStringFormatter.swift b/Shared/Extensions/ArticleStringFormatter.swift index 7cf9d0210..f99142754 100644 --- a/Shared/Extensions/ArticleStringFormatter.swift +++ b/Shared/Extensions/ArticleStringFormatter.swift @@ -33,10 +33,19 @@ import RSParser }() init() { + NotificationCenter.default.addObserver(self, selector: #selector(handleAppDidGoToBackground(_:)), name: .appDidGoToBackground, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleLowMemory(_:)), name: .lowMemory, object: nil) } + @objc func handleAppDidGoToBackground(_ notification: Notification) { + emptyCaches() + } + @objc func handleLowMemory(_ notification: Notification) { + emptyCaches() + } + + private func emptyCaches() { feedNameCache.removeAll() titleCache.removeAll() summaryCache.removeAll() diff --git a/Shared/Images/ImageDownloader.swift b/Shared/Images/ImageDownloader.swift index afa62c781..b630c6e56 100644 --- a/Shared/Images/ImageDownloader.swift +++ b/Shared/Images/ImageDownloader.swift @@ -31,9 +31,14 @@ extension Notification.Name { self.diskCache = BinaryDiskCache(folder: folder.path) self.queue = DispatchQueue(label: "ImageDownloader serial queue - \(folder.path)") + NotificationCenter.default.addObserver(self, selector: #selector(handleAppDidGoToBackground(_:)), name: .appDidGoToBackground, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleLowMemory(_:)), name: .lowMemory, object: nil) } + @objc func handleAppDidGoToBackground(_ notification: Notification) { + imageCache.removeAll() + } + @objc func handleLowMemory(_ notification: Notification) { imageCache.removeAll() }