mirror of
https://github.com/yattee/yattee.git
synced 2025-08-05 18:24:02 +00:00
Videos cache model
This commit is contained in:
37
Model/Cache/VideosCacheModel.swift
Normal file
37
Model/Cache/VideosCacheModel.swift
Normal file
@@ -0,0 +1,37 @@
|
||||
import Cache
|
||||
import Foundation
|
||||
import Logging
|
||||
import SwiftyJSON
|
||||
|
||||
struct VideosCacheModel {
|
||||
static let shared = VideosCacheModel()
|
||||
let logger = Logger(label: "stream.yattee.cache.videos")
|
||||
|
||||
static let jsonToDataTransformer: (JSON) -> Data = { try! $0.rawData() }
|
||||
static let jsonFromDataTransformer: (Data) -> JSON = { try! JSON(data: $0) }
|
||||
static let jsonTransformer = Transformer(toData: jsonToDataTransformer, fromData: jsonFromDataTransformer)
|
||||
|
||||
static let videosStorageDiskConfig = DiskConfig(name: "videos")
|
||||
static let vidoesStorageMemoryConfig = MemoryConfig()
|
||||
|
||||
let videosStorage = try! Storage<String, JSON>(
|
||||
diskConfig: Self.videosStorageDiskConfig,
|
||||
memoryConfig: Self.vidoesStorageMemoryConfig,
|
||||
transformer: Self.jsonTransformer
|
||||
)
|
||||
|
||||
func storeVideo(_ video: Video) {
|
||||
logger.info("caching \(video.cacheKey)")
|
||||
try? videosStorage.setObject(video.json, forKey: video.cacheKey)
|
||||
}
|
||||
|
||||
func retrieveVideo(_ cacheKey: String) -> Video? {
|
||||
logger.info("retrieving cache for \(cacheKey)")
|
||||
|
||||
if let json = try? videosStorage.object(forKey: cacheKey) {
|
||||
return Video.from(json)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user