mirror of
https://github.com/yattee/yattee.git
synced 2024-12-22 21:43:41 +00:00
31 lines
1.2 KiB
Swift
31 lines
1.2 KiB
Swift
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)
|
|
}
|
|
|
|
func removeAll() {
|
|
try? videoStorage?.removeAll()
|
|
try? urlBookmarksStorage?.removeAll()
|
|
}
|
|
}
|