mirror of
https://github.com/cranci1/Sora.git
synced 2026-04-21 08:32:00 +00:00
parent
243e6fe54d
commit
01d36394c6
2 changed files with 40 additions and 35 deletions
|
|
@ -5,23 +5,29 @@
|
||||||
// Created by doomsboygaming on 5/22/25
|
// Created by doomsboygaming on 5/22/25
|
||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import Kingfisher
|
import Kingfisher
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
/// Manages Kingfisher image caching configuration
|
||||||
class KingfisherCacheManager {
|
class KingfisherCacheManager {
|
||||||
private let jpegCompressionQuality: CGFloat = 0.7
|
|
||||||
|
|
||||||
static let shared = KingfisherCacheManager()
|
static let shared = KingfisherCacheManager()
|
||||||
private let maxDiskCacheSize: UInt = 64 * 1024 * 1024
|
|
||||||
|
/// Maximum disk cache size (default 500MB)
|
||||||
|
private let maxDiskCacheSize: UInt = 500 * 1024 * 1024
|
||||||
|
|
||||||
|
/// Maximum cache age (default 7 days)
|
||||||
private let maxCacheAgeInDays: TimeInterval = 7
|
private let maxCacheAgeInDays: TimeInterval = 7
|
||||||
|
|
||||||
|
/// UserDefaults keys
|
||||||
private let imageCachingEnabledKey = "imageCachingEnabled"
|
private let imageCachingEnabledKey = "imageCachingEnabled"
|
||||||
|
|
||||||
|
/// Whether image caching is enabled
|
||||||
var isCachingEnabled: Bool {
|
var isCachingEnabled: Bool {
|
||||||
get {
|
get {
|
||||||
UserDefaults.standard.object(forKey: imageCachingEnabledKey) == nil ?
|
// Default to true if not set
|
||||||
true : UserDefaults.standard.bool(forKey: imageCachingEnabledKey)
|
UserDefaults.standard.object(forKey: imageCachingEnabledKey) == nil ?
|
||||||
|
true : UserDefaults.standard.bool(forKey: imageCachingEnabledKey)
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
UserDefaults.standard.set(newValue, forKey: imageCachingEnabledKey)
|
UserDefaults.standard.set(newValue, forKey: imageCachingEnabledKey)
|
||||||
|
|
@ -31,46 +37,31 @@ class KingfisherCacheManager {
|
||||||
|
|
||||||
private init() {
|
private init() {
|
||||||
configureKingfisher()
|
configureKingfisher()
|
||||||
#if os(iOS)
|
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(clearMemoryCacheOnWarning), name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc private func clearMemoryCacheOnWarning() {
|
|
||||||
KingfisherManager.shared.cache.clearMemoryCache()
|
|
||||||
Logger.shared.log("Cleared memory cache due to memory warning", type: "Debug")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configure Kingfisher with appropriate caching settings
|
||||||
func configureKingfisher() {
|
func configureKingfisher() {
|
||||||
let cache = ImageCache.default
|
let cache = ImageCache.default
|
||||||
|
|
||||||
|
// Set disk cache size limit and expiration
|
||||||
cache.diskStorage.config.sizeLimit = isCachingEnabled ? maxDiskCacheSize : 0
|
cache.diskStorage.config.sizeLimit = isCachingEnabled ? maxDiskCacheSize : 0
|
||||||
cache.diskStorage.config.expiration = isCachingEnabled ?
|
cache.diskStorage.config.expiration = isCachingEnabled ?
|
||||||
.days(Int(maxCacheAgeInDays)) : .seconds(1)
|
.days(Int(maxCacheAgeInDays)) : .seconds(1) // 1 second means effectively disabled
|
||||||
|
|
||||||
cache.memoryStorage.config.totalCostLimit = isCachingEnabled ?
|
// Set memory cache size
|
||||||
12 * 1024 * 1024 : 0
|
cache.memoryStorage.config.totalCostLimit = isCachingEnabled ?
|
||||||
|
30 * 1024 * 1024 : 0 // 30MB memory cache when enabled
|
||||||
cache.memoryStorage.config.cleanInterval = 60
|
|
||||||
|
|
||||||
KingfisherManager.shared.downloader.downloadTimeout = 15.0
|
|
||||||
|
|
||||||
struct CustomJPEGCacheSerializer: CacheSerializer {
|
|
||||||
let compressionQuality: CGFloat
|
|
||||||
|
|
||||||
func data(with image: KFCrossPlatformImage, original: Data?) -> Data? {
|
// Configure clean interval
|
||||||
return image.kf.jpegData(compressionQuality: compressionQuality)
|
cache.memoryStorage.config.cleanInterval = 60 // Clean memory every 60 seconds
|
||||||
}
|
|
||||||
|
// Configure retry strategy
|
||||||
func image(with data: Data, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
|
KingfisherManager.shared.downloader.downloadTimeout = 15.0 // 15 second timeout
|
||||||
return DefaultCacheSerializer.default.image(with: data, options: options)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cache.diskStorage.config.cacheSerializer = CustomJPEGCacheSerializer(compressionQuality: jpegCompressionQuality)
|
|
||||||
|
|
||||||
Logger.shared.log("Configured Kingfisher cache. Enabled: \(isCachingEnabled)", type: "Debug")
|
Logger.shared.log("Configured Kingfisher cache. Enabled: \(isCachingEnabled)", type: "Debug")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Clear all cached images
|
||||||
func clearCache(completion: (() -> Void)? = nil) {
|
func clearCache(completion: (() -> Void)? = nil) {
|
||||||
KingfisherManager.shared.cache.clearMemoryCache()
|
KingfisherManager.shared.cache.clearMemoryCache()
|
||||||
KingfisherManager.shared.cache.clearDiskCache {
|
KingfisherManager.shared.cache.clearDiskCache {
|
||||||
|
|
@ -79,6 +70,8 @@ class KingfisherCacheManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Calculate current cache size
|
||||||
|
/// - Parameter completion: Closure to call with cache size in bytes
|
||||||
func calculateCacheSize(completion: @escaping (UInt) -> Void) {
|
func calculateCacheSize(completion: @escaping (UInt) -> Void) {
|
||||||
KingfisherManager.shared.cache.calculateDiskStorageSize { result in
|
KingfisherManager.shared.cache.calculateDiskStorageSize { result in
|
||||||
switch result {
|
switch result {
|
||||||
|
|
@ -91,9 +84,12 @@ class KingfisherCacheManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convert cache size to user-friendly string
|
||||||
|
/// - Parameter sizeInBytes: Size in bytes
|
||||||
|
/// - Returns: Formatted string (e.g., "5.2 MB")
|
||||||
static func formatCacheSize(_ sizeInBytes: UInt) -> String {
|
static func formatCacheSize(_ sizeInBytes: UInt) -> String {
|
||||||
let formatter = ByteCountFormatter()
|
let formatter = ByteCountFormatter()
|
||||||
formatter.countStyle = .file
|
formatter.countStyle = .file
|
||||||
return formatter.string(fromByteCount: Int64(sizeInBytes))
|
return formatter.string(fromByteCount: Int64(sizeInBytes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -10,6 +10,15 @@
|
||||||
"version": null
|
"version": null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"package": "Kingfisher",
|
||||||
|
"repositoryURL": "https://github.com/onevcat/Kingfisher.git",
|
||||||
|
"state": {
|
||||||
|
"branch": null,
|
||||||
|
"revision": "b6f62758f21a8c03cd64f4009c037cfa580a256e",
|
||||||
|
"version": "7.9.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"package": "MarqueeLabel",
|
"package": "MarqueeLabel",
|
||||||
"repositoryURL": "https://github.com/cbpowell/MarqueeLabel",
|
"repositoryURL": "https://github.com/cbpowell/MarqueeLabel",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue