2022-12-13 23:07:32 +00:00
|
|
|
import Cache
|
|
|
|
import Foundation
|
|
|
|
import Logging
|
|
|
|
import SwiftyJSON
|
|
|
|
|
|
|
|
struct ChannelsCacheModel: CacheModel {
|
2023-04-22 13:08:33 +00:00
|
|
|
static let shared = Self()
|
2022-12-13 23:07:32 +00:00
|
|
|
let logger = Logger(label: "stream.yattee.cache.channels")
|
|
|
|
|
|
|
|
static let diskConfig = DiskConfig(name: "channels")
|
|
|
|
static let memoryConfig = MemoryConfig()
|
|
|
|
|
|
|
|
let storage = try? Storage<String, JSON>(
|
|
|
|
diskConfig: Self.diskConfig,
|
|
|
|
memoryConfig: Self.memoryConfig,
|
2024-08-24 11:17:26 +00:00
|
|
|
fileManager: FileManager.default,
|
2022-12-13 23:07:32 +00:00
|
|
|
transformer: BaseCacheModel.jsonTransformer
|
|
|
|
)
|
|
|
|
|
|
|
|
func store(_ channel: Channel) {
|
|
|
|
guard channel.hasExtendedDetails else {
|
2022-12-20 20:41:27 +00:00
|
|
|
logger.debug("not caching \(channel.cacheKey)")
|
2022-12-13 23:07:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info("caching \(channel.cacheKey)")
|
|
|
|
try? storage?.setObject(channel.json, forKey: channel.cacheKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func storeIfMissing(_ channel: Channel) {
|
|
|
|
guard let storage, !storage.objectExists(forKey: channel.cacheKey) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
store(channel)
|
|
|
|
}
|
|
|
|
|
2023-02-28 20:03:02 +00:00
|
|
|
func retrieve(_ cacheKey: String) -> ChannelPage? {
|
2022-12-20 20:41:27 +00:00
|
|
|
logger.debug("retrieving cache for \(cacheKey)")
|
2022-12-13 23:07:32 +00:00
|
|
|
|
|
|
|
if let json = try? storage?.object(forKey: cacheKey) {
|
2023-02-28 20:03:02 +00:00
|
|
|
return ChannelPage(channel: Channel.from(json))
|
2022-12-13 23:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|