Skip to content

Commit 612766c

Browse files
Add new url cache system
1 parent 9d4870d commit 612766c

File tree

66 files changed

+6497
-191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+6497
-191
lines changed

Diff for: Cloudinary/Classes/Core/BaseNetwork/CLDNSessionManager.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ internal class CLDNSessionManager {
147147
/// `nil` by default.
148148
internal var backgroundCompletionHandler: (() -> Void)?
149149

150-
let queue = DispatchQueue(label: "org.cloudinary.session-manager." + UUID().uuidString)
150+
let queue = DispatchQueue(label: "com.cloudinary.session-manager." + UUID().uuidString)
151151

152152
// MARK: - Lifecycle
153153

Diff for: Cloudinary/Classes/Core/BaseNetwork/CLDNValidation.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extension CLDNRequest {
7676

7777
// MARK: Properties
7878

79-
fileprivate var acceptableStatusCodes: [Int] { return Array(200..<300) }
79+
internal var acceptableStatusCodes: [Int] { return Array(200..<300) }
8080

8181
fileprivate var acceptableContentTypes: [String] {
8282
if let accept = request?.value(forHTTPHeaderField: "Accept") {

Diff for: Cloudinary/Classes/Core/CLDCloudinary.swift

+70-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import Foundation
2626
import UIKit
2727

2828
public typealias CLDCompletionHandler = (_ responseImage: UIImage?, _ error: NSError?) -> ()
29+
public typealias CLDAssetCompletionHandler = (_ responseAsset: Data?, _ error: NSError?) -> ()
2930
public typealias CLDUploadCompletionHandler = (_ response: CLDUploadResult?, _ error: NSError?) -> ()
3031

3132
@objcMembers open class CLDCloudinary: NSObject {
@@ -40,6 +41,10 @@ public typealias CLDUploadCompletionHandler = (_ response: CLDUploadResult?, _ e
4041
*/
4142
fileprivate var networkCoordinator: CLDNetworkCoordinator
4243

44+
/**
45+
The network download coordinator coordinates between the SDK's API level classes to its network adapter layer.
46+
*/
47+
fileprivate var downloadCoordinator: CLDDownloadCoordinator
4348

4449
// MARK: - SDK Configurations
4550

@@ -65,23 +70,23 @@ public typealias CLDUploadCompletionHandler = (_ response: CLDUploadResult?, _ e
6570
*/
6671
open var cachePolicy: CLDImageCachePolicy {
6772
get {
68-
return CLDImageCache.defaultImageCache.cachePolicy
73+
return downloadCoordinator.imageCache.cachePolicy
6974
}
7075
set {
71-
CLDImageCache.defaultImageCache.cachePolicy = newValue
76+
downloadCoordinator.imageCache.cachePolicy = newValue
7277
}
7378
}
7479

7580
/**
7681
Sets Cloudinary SDK's image cache maximum disk capacity.
77-
default is 150 MB.
82+
default is 150 MB.
7883
*/
7984
open var cacheMaxDiskCapacity: UInt64 {
8085
get {
81-
return CLDImageCache.defaultImageCache.maxDiskCapacity
86+
return downloadCoordinator.imageCache.maxDiskCapacity
8287
}
8388
set {
84-
CLDImageCache.defaultImageCache.maxDiskCapacity = newValue
89+
downloadCoordinator.imageCache.maxDiskCapacity = newValue
8590
}
8691
}
8792

@@ -91,21 +96,47 @@ public typealias CLDUploadCompletionHandler = (_ response: CLDUploadResult?, _ e
9196
*/
9297
open var cacheMaxMemoryTotalCost: Int {
9398
get {
94-
return CLDImageCache.defaultImageCache.maxMemoryTotalCost
99+
return downloadCoordinator.imageCache.maxMemoryTotalCost
100+
}
101+
set {
102+
downloadCoordinator.imageCache.maxMemoryTotalCost = newValue
103+
}
104+
}
105+
106+
/**
107+
Sets Cloudinary SDK's asset cache maximum disk capacity.
108+
default is 150 MB.
109+
*/
110+
open var cacheAssetMaxDiskCapacity: Int {
111+
get {
112+
return downloadCoordinator.urlCache.diskCapacity
95113
}
96114
set {
97-
CLDImageCache.defaultImageCache.maxMemoryTotalCost = newValue
115+
downloadCoordinator.urlCache.updateDiskCapacity(newValue)
98116
}
99117
}
100118

119+
/**
120+
Sets Cloudinary SDK's asset cache maximum memory total cost.
121+
default is 30 MB.
122+
*/
123+
open var cacheAssetMaxMemoryTotalCost: Int {
124+
get {
125+
return downloadCoordinator.urlCache.memoryCapacity
126+
}
127+
set {
128+
downloadCoordinator.urlCache.updateMemoryCapacity(newValue)
129+
}
130+
}
131+
101132
/**
102133
Removes an image from the downloaded images cache, both disk and memory.
103134

104135
- parameter key: The full url of the image to remove.
105136

106137
*/
107138
open func removeFromCache(key: String) {
108-
CLDImageCache.defaultImageCache.removeCacheImageForKey(key)
139+
downloadCoordinator.imageCache.removeCacheImageForKey(key)
109140
}
110141

111142
// MARK: - Init
@@ -118,7 +149,25 @@ public typealias CLDUploadCompletionHandler = (_ response: CLDUploadResult?, _ e
118149

119150
- returns: The new `CLDCloudinary` instance.
120151
*/
121-
public init(configuration: CLDConfiguration, networkAdapter: CLDNetworkAdapter? = nil, sessionConfiguration: URLSessionConfiguration? = nil) {
152+
public convenience init(configuration: CLDConfiguration, networkAdapter: CLDNetworkAdapter? = nil, sessionConfiguration: URLSessionConfiguration? = nil) {
153+
154+
self.init(configuration: configuration, networkAdapter: networkAdapter, downloadAdapter: nil, sessionConfiguration: sessionConfiguration, downloadSessionConfiguration: nil)
155+
}
156+
157+
/**
158+
Initializes the `CLDCloudinary` instance with the specified configuration and network adapter.
159+
160+
- parameter configuration: The configuration used by this CLDCloudinary instance.
161+
- parameter networkAdapter: A network adapter that implements `CLDNetworkAdapter`.
162+
- parameter downloadAdapter: A download adapter that implements `CLDNetworkAdapter`.
163+
- parameter sessionConfiguration: A session configuration that implements `URLSessionConfiguration`.
164+
- parameter downloadSessionConfiguration: A download session configuration that implements `URLSessionConfiguration`.
165+
CLDNetworkDelegate() by default.
166+
167+
- returns: The new `CLDCloudinary` instance.
168+
*/
169+
public init(configuration: CLDConfiguration, networkAdapter: CLDNetworkAdapter? = nil, downloadAdapter: CLDNetworkAdapter? = nil, sessionConfiguration: URLSessionConfiguration? = nil, downloadSessionConfiguration: URLSessionConfiguration? = nil) {
170+
122171
config = configuration
123172
if let customNetworkAdapter = networkAdapter {
124173
networkCoordinator = CLDNetworkCoordinator(configuration: config, networkAdapter: customNetworkAdapter)
@@ -129,6 +178,17 @@ public typealias CLDUploadCompletionHandler = (_ response: CLDUploadResult?, _ e
129178
networkCoordinator = CLDNetworkCoordinator(configuration: config)
130179
}
131180
}
181+
182+
if let customDownloadAdapter = downloadAdapter {
183+
downloadCoordinator = CLDDownloadCoordinator(configuration: config, networkAdapter: customDownloadAdapter)
184+
} else {
185+
if let downloadSessionConfiguration = downloadSessionConfiguration {
186+
downloadCoordinator = CLDDownloadCoordinator(configuration: config, sessionConfiguration: downloadSessionConfiguration)
187+
} else {
188+
downloadCoordinator = CLDDownloadCoordinator(configuration: config)
189+
}
190+
}
191+
132192
super.init()
133193
}
134194

@@ -158,7 +218,7 @@ public typealias CLDUploadCompletionHandler = (_ response: CLDUploadResult?, _ e
158218
- returns: A new `CLDDownloader` instance.
159219
*/
160220
open func createDownloader() -> CLDDownloader {
161-
return CLDDownloader(networkCoordinator: networkCoordinator)
221+
return CLDDownloader(downloadCoordinator: downloadCoordinator)
162222
}
163223

164224
/**

0 commit comments

Comments
 (0)