-
Notifications
You must be signed in to change notification settings - Fork 16
/
CachyLoader.swift
202 lines (181 loc) · 6.8 KB
/
CachyLoader.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// A.swift
// Cachy
//
// Created by sadman samee on 9/4/19.
// Copyright © 2019 sadman samee. All rights reserved.
//
import Foundation
import UIKit
public typealias CachyCallback = (Data, URL) -> Void
public typealias CachyImageCallback = (UIImage, URL) -> Void
public typealias CachyCallbackList = [CachyCallback]
open class CachyLoaderManager {
public static let shared: CachyLoaderManager = {
let instance = CachyLoaderManager()
return instance
}()
fileprivate var cache: Cachy
private var fetchList: [String: CachyCallbackList] = [:]
private var fetchListOperationQueue: DispatchQueue = DispatchQueue(label: "cachy.awesome.fetchlist_queue",
attributes: DispatchQueue.Attributes.concurrent)
private var sessionConfiguration: URLSessionConfiguration!
private var sessionQueue: OperationQueue!
fileprivate lazy var defaultSession: URLSession! = URLSession(configuration:
self.sessionConfiguration, delegate: nil,
delegateQueue: self.sessionQueue)
public func configure(memoryCapacity: Int = 30 * 1024 * 1024,
maxConcurrentOperationCount: Int = 10,
timeoutIntervalForRequest: Double = 3,
expiryDate: ExpiryDate = .everyWeek,
isOnlyInMemory: Bool = false,
isSupportingSecureCodingSaving: Bool = true) {
cache.totalCostLimit = memoryCapacity
cache.expiration = expiryDate
Cachy.isOnlyInMemory = isOnlyInMemory
Cachy.isSupportingSecureCodingSaving = isSupportingSecureCodingSaving
sessionQueue = OperationQueue()
sessionQueue.maxConcurrentOperationCount = maxConcurrentOperationCount
sessionQueue.name = "cachy.awesome.session"
sessionConfiguration = URLSessionConfiguration.default
sessionConfiguration.requestCachePolicy = .useProtocolCachePolicy
sessionConfiguration.timeoutIntervalForRequest = timeoutIntervalForRequest
}
private init(memoryCapacity: Int = 30 * 1024 * 1024,
maxConcurrentOperationCount: Int = 10,
timeoutIntervalForRequest: Double = 3,
expiryDate: ExpiryDate = .everyWeek) {
cache = Cachy()
configure(memoryCapacity: memoryCapacity,
maxConcurrentOperationCount: maxConcurrentOperationCount,
timeoutIntervalForRequest: timeoutIntervalForRequest,
expiryDate: expiryDate)
}
}
extension CachyLoaderManager {
fileprivate func readFetch(_ key: String) -> CachyCallbackList? {
return fetchList[key]
}
fileprivate func addFetch(_ key: String, callback: @escaping CachyCallback) -> Bool {
var skip = false
let list = fetchList[key]
if list != nil {
skip = true
}
fetchListOperationQueue.sync(flags: .barrier, execute: {
if var fList = list {
fList.append(callback)
self.fetchList[key] = fList
} else {
self.fetchList[key] = [callback]
}
})
return skip
}
fileprivate func removeFetch(_ key: String) {
_ = fetchListOperationQueue.sync(flags: .barrier) {
self.fetchList.removeValue(forKey: key)
}
}
fileprivate func clearFetch() {
fetchListOperationQueue.async(flags: .barrier) {
self.fetchList.removeAll()
}
}
}
extension CachyLoaderManager {
public func clear() {
cache.clear()
sessionConfiguration.urlCache?.removeAllCachedResponses()
}
}
// MARK: - CachyLoader
open class CachyLoader: NSObject {
var task: URLSessionTask?
public override init() {
super.init()
}
}
extension CachyLoader {
fileprivate func cacheKeyFromUrl(url: URL) -> String? {
let path = url.absoluteString
let cacheKey = path
return cacheKey
}
fileprivate func dataFromFastCache(cacheKey: String) -> Data? {
return CachyLoaderManager.shared.cache.get(forKey: cacheKey)
}
public func loadWithURLRequest(_ urlRequest: URLRequest,
isRefresh: Bool = false,
expirationDate: Date? = nil,
callback: @escaping CachyCallback) {
guard let url = urlRequest.url else {
return
}
load(url: url,
urlRequest: urlRequest,
isRefresh: isRefresh,
expirationDate: expirationDate,
callback: callback)
}
public func loadWithURL(_ url: URL,
isRefresh: Bool = false,
expirationDate: Date? = nil,
callback: @escaping CachyCallback) {
load(url: url,
isRefresh: isRefresh,
expirationDate: expirationDate,
callback: callback)
}
private func load(url: URL,
urlRequest: URLRequest? = nil,
isRefresh: Bool = false,
expirationDate: Date? = nil,
callback: @escaping CachyCallback) {
guard let fetchKey = self.cacheKeyFromUrl(url: url as URL) else {
return
}
if !isRefresh {
if let data = self.dataFromFastCache(cacheKey: fetchKey) {
callback(data, url)
return
}
}
let cacheCallback = {
(data: Data) -> Void in
if let fetchList = CachyLoaderManager.shared.readFetch(fetchKey) {
CachyLoaderManager.shared.removeFetch(fetchKey)
DispatchQueue.main.async {
for f in fetchList {
f(data, url)
}
}
}
}
let skip = CachyLoaderManager.shared.addFetch(fetchKey, callback: callback)
if skip {
return
}
let session = CachyLoaderManager.shared.defaultSession
let request = urlRequest ?? URLRequest(url: url)
task = session?.dataTask(with: request, completionHandler: { data, _, _ in
guard let data = data else {
return
}
let object = CachyObject(value: data as NSData, key: fetchKey, expirationDate: expirationDate)
CachyLoaderManager.shared.cache.add(object: object)
cacheCallback(data)
})
task?.resume()
}
}
extension CachyLoader {
public func cancelTask() {
guard let _task = self.task else {
return
}
if _task.state == .running || _task.state == .running {
_task.cancel()
}
}
}