1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Model/Cache/ChannelPlaylistsCacheModel.swift

57 lines
1.9 KiB
Swift
Raw Normal View History

2022-12-11 23:14:55 +05:30
import Cache
import Foundation
import Logging
import SwiftyJSON
2022-12-12 14:51:46 +05:30
struct ChannelPlaylistsCacheModel: CacheModel {
2022-12-11 23:14:55 +05:30
static let shared = ChannelPlaylistsCacheModel()
let logger = Logger(label: "stream.yattee.cache.channel-playlists")
static let diskConfig = DiskConfig(name: "channel-playlists")
static let memoryConfig = MemoryConfig()
2022-12-12 14:51:46 +05:30
var storage = try? Storage<String, JSON>(
2022-12-11 23:14:55 +05:30
diskConfig: Self.diskConfig,
memoryConfig: Self.memoryConfig,
2022-12-12 14:51:46 +05:30
transformer: BaseCacheModel.jsonTransformer
2022-12-11 23:14:55 +05:30
)
func storePlaylist(playlist: ChannelPlaylist) {
2022-12-12 14:51:46 +05:30
let date = iso8601DateFormatter.string(from: Date())
2022-12-17 01:07:05 +05:30
logger.info("STORE \(playlist.cacheKey) -- \(date)")
2022-12-11 23:14:55 +05:30
let feedTimeObject: JSON = ["date": date]
let playlistObject: JSON = ["playlist": playlist.json.object]
2022-12-17 01:07:05 +05:30
try? storage?.setObject(feedTimeObject, forKey: playlistTimeCacheKey(playlist.cacheKey))
try? storage?.setObject(playlistObject, forKey: playlist.cacheKey)
2022-12-11 23:14:55 +05:30
}
2022-12-17 01:07:05 +05:30
func retrievePlaylist(_ playlist: ChannelPlaylist) -> ChannelPlaylist? {
logger.info("RETRIEVE \(playlist.cacheKey)")
2022-12-11 23:14:55 +05:30
2022-12-17 01:07:05 +05:30
if let json = try? storage?.object(forKey: playlist.cacheKey).dictionaryValue["playlist"] {
2022-12-11 23:14:55 +05:30
return ChannelPlaylist.from(json)
}
return nil
}
func getPlaylistsTime(_ id: ChannelPlaylist.ID) -> Date? {
2022-12-12 14:51:46 +05:30
if let json = try? storage?.object(forKey: playlistTimeCacheKey(id)),
2022-12-11 23:14:55 +05:30
let string = json.dictionaryValue["date"]?.string,
2022-12-12 14:51:46 +05:30
let date = iso8601DateFormatter.date(from: string)
2022-12-11 23:14:55 +05:30
{
return date
}
return nil
}
func getFormattedPlaylistTime(_ id: ChannelPlaylist.ID) -> String {
2022-12-12 14:51:46 +05:30
getFormattedDate(getPlaylistsTime(id))
2022-12-11 23:14:55 +05:30
}
2022-12-17 01:07:05 +05:30
private func playlistTimeCacheKey(_ cacheKey: ChannelPlaylist.ID) -> String {
"\(cacheKey)-time"
2022-12-11 23:14:55 +05:30
}
}