yattee/Model/CacheModel.swift

31 lines
1.2 KiB
Swift
Raw Normal View History

2022-11-10 17:11:28 +00:00
import Cache
import Foundation
import SwiftyJSON
struct CacheModel {
static var shared = CacheModel()
var urlBookmarksStorage: Storage<String, Data>?
var videoStorage: Storage<Video.ID, JSON>?
init() {
let urlBookmarksStorageConfig = DiskConfig(name: "URLBookmarks", expiry: .never)
let urlBookmarksMemoryConfig = MemoryConfig(expiry: .never, countLimit: 100, totalCostLimit: 100)
urlBookmarksStorage = try? Storage(diskConfig: urlBookmarksStorageConfig, memoryConfig: urlBookmarksMemoryConfig, transformer: TransformerFactory.forData())
let videoStorageConfig = DiskConfig(name: "VideoStorage", expiry: .never)
let videoStorageMemoryConfig = MemoryConfig(expiry: .never, countLimit: 100, totalCostLimit: 100)
let toData: (JSON) throws -> Data = { try $0.rawData() }
let fromData: (Data) throws -> JSON = { try JSON(data: $0) }
let jsonTransformer = Transformer<JSON>(toData: toData, fromData: fromData)
videoStorage = try? Storage<Video.ID, JSON>(diskConfig: videoStorageConfig, memoryConfig: videoStorageMemoryConfig, transformer: jsonTransformer)
}
2022-11-10 20:46:37 +00:00
func removeAll() {
try? videoStorage?.removeAll()
try? urlBookmarksStorage?.removeAll()
}
2022-11-10 17:11:28 +00:00
}