yattee/Model/Cache/VideosCacheModel.swift

36 lines
957 B
Swift
Raw Normal View History

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