From 6a379571211ff00477fdef6a7a5cda5dbfb305a2 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Thu, 7 Dec 2023 16:58:39 -0600 Subject: [PATCH] =?UTF-8?q?Clears=20image=20cache=20with=20=E2=80=9CClear?= =?UTF-8?q?=20Cache=E2=80=A6=E2=80=9D=20option=20in=20Settings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also increases image cache size to 512MB. --- AltStore/AppDelegate.swift | 30 +++++++++++++++++++ .../Operations/ClearAppCacheOperation.swift | 10 +++++++ 2 files changed, 40 insertions(+) diff --git a/AltStore/AppDelegate.swift b/AltStore/AppDelegate.swift index 671c9679..6a9a90d6 100644 --- a/AltStore/AppDelegate.swift +++ b/AltStore/AppDelegate.swift @@ -15,6 +15,8 @@ import AltStoreCore import AltSign import Roxas +import Nuke + extension UIApplication: LegacyBackgroundFetching {} extension AppDelegate @@ -57,6 +59,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { AnalyticsManager.shared.start() self.setTintColor() + self.prepareImageCache() ServerManager.shared.startDiscovering() @@ -145,6 +148,33 @@ private extension AppDelegate self.window?.tintColor = .altPrimary } + func prepareImageCache() + { + // Avoid caching responses twice. + DataLoader.sharedUrlCache.diskCapacity = 0 + + let pipeline = ImagePipeline { configuration in + do + { + let dataCache = try DataCache(name: "io.altstore.Nuke") + dataCache.sizeLimit = 512 * 1024 * 1024 // 512MB + + configuration.dataCache = dataCache + } + catch + { + Logger.main.error("Failed to create image disk cache. Falling back to URL cache. \(error.localizedDescription, privacy: .public)") + } + } + + ImagePipeline.shared = pipeline + + if let dataCache = ImagePipeline.shared.configuration.dataCache as? DataCache, #available(iOS 15, *) + { + Logger.main.info("Current image cache size: \(dataCache.totalSize.formatted(.byteCount(style: .file)), privacy: .public)") + } + } + func open(_ url: URL) -> Bool { if url.isFileURL diff --git a/AltStore/Operations/ClearAppCacheOperation.swift b/AltStore/Operations/ClearAppCacheOperation.swift index d8c30c8a..abf82228 100644 --- a/AltStore/Operations/ClearAppCacheOperation.swift +++ b/AltStore/Operations/ClearAppCacheOperation.swift @@ -9,6 +9,8 @@ import Foundation import AltStoreCore +import Nuke + struct BatchError: ALTLocalizedError { enum Code: Int, ALTErrorCode @@ -54,6 +56,8 @@ class ClearAppCacheOperation: ResultOperation { super.main() + self.clearNukeCache() + var allErrors = [Error]() self.clearTemporaryDirectory { result in @@ -88,6 +92,12 @@ class ClearAppCacheOperation: ResultOperation private extension ClearAppCacheOperation { + func clearNukeCache() + { + guard let dataCache = ImagePipeline.shared.configuration.dataCache as? DataCache else { return } + dataCache.removeAll() + } + func clearTemporaryDirectory(completion: @escaping (Result) -> Void) { let intent = NSFileAccessIntent.writingIntent(with: FileManager.default.temporaryDirectory, options: [.forDeleting])