yattee/Model/Cache/VideosCacheModel.swift

34 lines
903 B
Swift
Raw Normal View History

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