2018-10-02 17:42:37 +00:00
|
|
|
//
|
|
|
|
// ImageCache.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 8/21/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Cache
|
|
|
|
|
|
|
|
class ImageCache {
|
|
|
|
|
2018-11-08 00:24:52 +00:00
|
|
|
static let avatars = ImageCache(name: "Avatars", memoryExpiry: .seconds(60 * 60), diskExpiry: .seconds(60 * 60 * 24))
|
|
|
|
static let headers = ImageCache(name: "Headers", memoryExpiry: .seconds(60 * 60), diskExpiry: .seconds(60 * 60 * 24))
|
|
|
|
static let attachments = ImageCache(name: "Attachments", memoryExpiry: .seconds(60 * 2))
|
2019-02-10 02:12:31 +00:00
|
|
|
static let emojis = ImageCache(name: "Emojis", memoryExpiry: .seconds(60 * 5), diskExpiry: .seconds(60 * 60))
|
2018-10-02 17:42:37 +00:00
|
|
|
|
2018-11-09 20:48:08 +00:00
|
|
|
let cache: Cache<Data>
|
2018-10-02 17:42:37 +00:00
|
|
|
|
|
|
|
var requests = [URL: Request]()
|
|
|
|
|
2018-11-08 00:24:52 +00:00
|
|
|
init(name: String, memoryExpiry expiry: Expiry) {
|
2018-11-09 20:48:08 +00:00
|
|
|
let storage = MemoryStorage<Data>(config: MemoryConfig(expiry: expiry))
|
2018-11-08 00:24:52 +00:00
|
|
|
self.cache = .memory(storage)
|
|
|
|
}
|
|
|
|
|
|
|
|
init(name: String, diskExpiry expiry: Expiry) {
|
2018-11-09 20:48:08 +00:00
|
|
|
let storage = try! DiskStorage<Data>(config: DiskConfig(name: name, expiry: expiry), transformer: TransformerFactory.forData())
|
2018-11-08 00:24:52 +00:00
|
|
|
self.cache = .disk(storage)
|
|
|
|
}
|
|
|
|
|
|
|
|
init(name: String, memoryExpiry: Expiry, diskExpiry: Expiry) {
|
2018-11-09 20:48:08 +00:00
|
|
|
let memory = MemoryStorage<Data>(config: MemoryConfig(expiry: memoryExpiry))
|
|
|
|
let disk = try! DiskStorage<Data>(config: DiskConfig(name: name, expiry: diskExpiry), transformer: TransformerFactory.forData())
|
2018-11-08 00:24:52 +00:00
|
|
|
self.cache = .hybrid(HybridStorage(memoryStorage: memory, diskStorage: disk))
|
2018-10-02 17:42:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 20:48:08 +00:00
|
|
|
func get(_ url: URL, completion: ((Data?) -> Void)?) {
|
2018-10-02 17:42:37 +00:00
|
|
|
let key = url.absoluteString
|
2018-11-08 00:24:52 +00:00
|
|
|
if (try? cache.existsObject(forKey: key)) ?? false,
|
2018-11-09 20:48:08 +00:00
|
|
|
let data = try? cache.object(forKey: key) {
|
|
|
|
completion?(data)
|
2018-10-02 17:42:37 +00:00
|
|
|
} else {
|
|
|
|
if let completion = completion, let request = requests[url] {
|
|
|
|
request.callbacks.append(completion)
|
|
|
|
} else {
|
|
|
|
let request = Request(url: url, completion: completion)
|
|
|
|
requests[url] = request
|
2018-11-09 20:48:08 +00:00
|
|
|
request.run { (data) in
|
|
|
|
try? self.cache.setObject(data, forKey: key)
|
2018-10-02 17:42:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 20:48:08 +00:00
|
|
|
func get(_ url: URL) -> Data? {
|
2018-11-08 00:24:52 +00:00
|
|
|
return try? cache.object(forKey: url.absoluteString)
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 17:42:37 +00:00
|
|
|
func cancel(_ url: URL) {
|
|
|
|
requests[url]?.cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
class Request {
|
|
|
|
let url: URL
|
|
|
|
var task: URLSessionDataTask?
|
2018-11-09 20:48:08 +00:00
|
|
|
var callbacks: [(Data?) -> Void]
|
2018-10-02 17:42:37 +00:00
|
|
|
|
2018-11-09 20:48:08 +00:00
|
|
|
init(url: URL, completion: ((Data?) -> Void)?) {
|
2018-10-02 17:42:37 +00:00
|
|
|
if let completion = completion {
|
|
|
|
self.callbacks = [completion]
|
|
|
|
} else {
|
|
|
|
self.callbacks = []
|
|
|
|
}
|
|
|
|
self.url = url
|
|
|
|
}
|
|
|
|
|
2018-11-09 20:48:08 +00:00
|
|
|
func run(cache: @escaping (Data) -> Void) {
|
2018-10-02 17:42:37 +00:00
|
|
|
task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
|
2018-11-09 20:48:08 +00:00
|
|
|
guard error == nil, let data = data else {
|
2018-10-02 17:42:37 +00:00
|
|
|
self.complete(with: nil)
|
|
|
|
return
|
|
|
|
}
|
2018-11-09 20:48:08 +00:00
|
|
|
cache(data)
|
|
|
|
self.complete(with: data)
|
2018-10-02 17:42:37 +00:00
|
|
|
})
|
|
|
|
task!.resume()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cancel() {
|
|
|
|
task?.cancel()
|
|
|
|
complete(with: nil)
|
|
|
|
}
|
|
|
|
|
2018-11-09 20:48:08 +00:00
|
|
|
func complete(with data: Data?) {
|
|
|
|
callbacks.forEach { $0(data) }
|
2018-10-02 17:42:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|