1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 22:30:32 +05:30
yattee/Model/Cache/VideosCacheModel.swift

37 lines
999 B
Swift
Raw Permalink Normal View History

2022-12-10 05:53:13 +05:30
import Cache
import Foundation
import Logging
import SwiftyJSON
2022-12-12 14:51:46 +05:30
struct VideosCacheModel: CacheModel {
2023-04-22 18:38:33 +05:30
static let shared = Self()
2022-12-10 05:53:13 +05:30
let logger = Logger(label: "stream.yattee.cache.videos")
2022-12-10 07:31:59 +05:30
static let diskConfig = DiskConfig(name: "videos")
static let memoryConfig = MemoryConfig()
2022-12-10 05:53:13 +05:30
2022-12-12 14:51:46 +05:30
let storage = try? Storage<String, JSON>(
2022-12-10 07:31:59 +05:30
diskConfig: Self.diskConfig,
memoryConfig: Self.memoryConfig,
fileManager: FileManager.default,
2022-12-12 14:51:46 +05:30
transformer: BaseCacheModel.jsonTransformer
2022-12-10 05:53:13 +05:30
)
func storeVideo(_ video: Video) {
logger.info("caching \(video.cacheKey)")
2022-12-12 14:51:46 +05:30
try? storage?.setObject(video.json, forKey: video.cacheKey)
2022-12-14 04:37:32 +05:30
ChannelsCacheModel.shared.storeIfMissing(video.channel)
2022-12-10 05:53:13 +05:30
}
func retrieveVideo(_ cacheKey: String) -> Video? {
2022-12-21 02:11:27 +05:30
logger.debug("retrieving cache for \(cacheKey)")
2022-12-10 05:53:13 +05:30
2022-12-12 14:51:46 +05:30
if let json = try? storage?.object(forKey: cacheKey) {
2022-12-10 05:53:13 +05:30
return Video.from(json)
}
return nil
}
}