Cache fixes

This commit is contained in:
Arkadiusz Fal
2022-12-12 10:21:46 +01:00
parent 25da312966
commit a35d697ebe
10 changed files with 119 additions and 118 deletions

View File

@@ -3,35 +3,31 @@ import Foundation
import Logging
import SwiftyJSON
struct VideosCacheModel {
struct VideosCacheModel: CacheModel {
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>(
let storage = try? Storage<String, JSON>(
diskConfig: Self.diskConfig,
memoryConfig: Self.memoryConfig,
transformer: CacheModel.jsonTransformer
transformer: BaseCacheModel.jsonTransformer
)
func storeVideo(_ video: Video) {
logger.info("caching \(video.cacheKey)")
try? storage.setObject(video.json, forKey: 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) {
if let json = try? storage?.object(forKey: cacheKey) {
return Video.from(json)
}
return nil
}
func clear() {
try? storage.removeAll()
}
}