1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Model/Cache/BaseCacheModel.swift

42 lines
1.2 KiB
Swift
Raw Normal View History

2022-12-12 14:51:46 +05:30
import Cache
import Foundation
import Logging
import SwiftyJSON
struct BaseCacheModel {
2023-04-22 18:38:33 +05:30
static var shared = Self()
2022-12-12 14:51:46 +05:30
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-17 03:07:30 +05:30
static let imageCache = URLCache(memoryCapacity: 512 * 1000 * 1000, diskCapacity: 10 * 1000 * 1000 * 1000)
2022-12-12 14:51:46 +05:30
var models: [CacheModel] {
[
FeedCacheModel.shared,
VideosCacheModel.shared,
2022-12-14 04:37:32 +05:30
ChannelsCacheModel.shared,
2022-12-12 14:51:46 +05:30
PlaylistsCacheModel.shared,
ChannelPlaylistsCacheModel.shared,
SubscribedChannelsModel.shared
]
}
func clear() {
models.forEach { $0.clear() }
2022-12-17 03:07:30 +05:30
Self.imageCache.removeAllCachedResponses()
2022-12-12 14:51:46 +05:30
}
var totalSize: Int {
2022-12-17 03:07:30 +05:30
models.compactMap { $0.storage?.totalDiskStorageSize }.reduce(0, +) + Self.imageCache.currentDiskUsage
2022-12-12 14:51:46 +05:30
}
var totalSizeFormatted: String {
byteCountFormatter.string(fromByteCount: Int64(totalSize))
}
private var byteCountFormatter: ByteCountFormatter { .init() }
}