1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Model/Cache/ChannelsCacheModel.swift

48 lines
1.3 KiB
Swift
Raw Normal View History

2022-12-14 04:37:32 +05:30
import Cache
import Foundation
import Logging
import SwiftyJSON
struct ChannelsCacheModel: CacheModel {
2023-04-22 18:38:33 +05:30
static let shared = Self()
2022-12-14 04:37:32 +05:30
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,
fileManager: FileManager.default,
2022-12-14 04:37:32 +05:30
transformer: BaseCacheModel.jsonTransformer
)
func store(_ channel: Channel) {
guard channel.hasExtendedDetails else {
2022-12-21 02:11:27 +05:30
logger.debug("not caching \(channel.cacheKey)")
2022-12-14 04:37:32 +05:30
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)
}
func retrieve(_ cacheKey: String) -> ChannelPage? {
2022-12-21 02:11:27 +05:30
logger.debug("retrieving cache for \(cacheKey)")
2022-12-14 04:37:32 +05:30
if let json = try? storage?.object(forKey: cacheKey) {
return ChannelPage(channel: Channel.from(json))
2022-12-14 04:37:32 +05:30
}
return nil
}
}