mirror of
https://github.com/yattee/yattee.git
synced 2024-12-13 22:00:31 +05:30
38 lines
944 B
Swift
38 lines
944 B
Swift
import Cache
|
|
import Foundation
|
|
import Logging
|
|
import SwiftyJSON
|
|
|
|
struct VideosCacheModel {
|
|
static let shared = VideosCacheModel()
|
|
let logger = Logger(label: "stream.yattee.cache.videos")
|
|
|
|
static let diskConfig = DiskConfig(name: "videos")
|
|
static let memoryConfig = MemoryConfig()
|
|
|
|
let storage = try! Storage<String, JSON>(
|
|
diskConfig: Self.diskConfig,
|
|
memoryConfig: Self.memoryConfig,
|
|
transformer: CacheModel.jsonTransformer
|
|
)
|
|
|
|
func storeVideo(_ video: Video) {
|
|
logger.info("caching \(video.cacheKey)")
|
|
try? storage.setObject(video.json, forKey: video.cacheKey)
|
|
}
|
|
|
|
func retrieveVideo(_ cacheKey: String) -> Video? {
|
|
logger.info("retrieving cache for \(cacheKey)")
|
|
|
|
if let json = try? storage.object(forKey: cacheKey) {
|
|
return Video.from(json)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func clear() {
|
|
try? storage.removeAll()
|
|
}
|
|
}
|