2022-12-12 09:21:46 +00:00
|
|
|
import Cache
|
|
|
|
import Foundation
|
|
|
|
import Logging
|
|
|
|
import SwiftyJSON
|
|
|
|
|
|
|
|
struct BaseCacheModel {
|
2023-04-22 13:08:33 +00:00
|
|
|
static var shared = Self()
|
2022-12-12 09:21:46 +00:00
|
|
|
|
|
|
|
static let jsonToDataTransformer: (JSON) -> Data = { try! $0.rawData() }
|
|
|
|
static let jsonFromDataTransformer: (Data) -> JSON = { try! JSON(data: $0) }
|
|
|
|
static let jsonTransformer = Transformer(toData: jsonToDataTransformer, fromData: jsonFromDataTransformer)
|
|
|
|
|
2022-12-16 21:37:30 +00:00
|
|
|
static let imageCache = URLCache(memoryCapacity: 512 * 1000 * 1000, diskCapacity: 10 * 1000 * 1000 * 1000)
|
|
|
|
|
2022-12-12 09:21:46 +00:00
|
|
|
var models: [CacheModel] {
|
|
|
|
[
|
|
|
|
FeedCacheModel.shared,
|
|
|
|
VideosCacheModel.shared,
|
2022-12-13 23:07:32 +00:00
|
|
|
ChannelsCacheModel.shared,
|
2022-12-12 09:21:46 +00:00
|
|
|
PlaylistsCacheModel.shared,
|
|
|
|
ChannelPlaylistsCacheModel.shared,
|
|
|
|
SubscribedChannelsModel.shared
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
func clear() {
|
|
|
|
models.forEach { $0.clear() }
|
2022-12-16 21:37:30 +00:00
|
|
|
|
|
|
|
Self.imageCache.removeAllCachedResponses()
|
2022-12-12 09:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var totalSize: Int {
|
2022-12-16 21:37:30 +00:00
|
|
|
models.compactMap { $0.storage?.totalDiskStorageSize }.reduce(0, +) + Self.imageCache.currentDiskUsage
|
2022-12-12 09:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var totalSizeFormatted: String {
|
|
|
|
byteCountFormatter.string(fromByteCount: Int64(totalSize))
|
|
|
|
}
|
|
|
|
|
|
|
|
private var byteCountFormatter: ByteCountFormatter { .init() }
|
|
|
|
}
|