Skip to content

Commit

Permalink
缓存同步
Browse files Browse the repository at this point in the history
  • Loading branch information
mengqingzheng committed Dec 27, 2021
1 parent ff116d4 commit f15a902
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
2 changes: 1 addition & 1 deletion DaisyNet.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = "DaisyNet"
s.version = "1.0.2"
s.version = "1.0.3"
s.summary = "Alamofire与Cache封装, 更容易存储请求数据"
s.homepage = "https://github.com/MQZHot/DaisyNet"
s.license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions Example/DaisyNetExample/MainTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ class MainTableViewController: UITableViewController {
DaisyNet.removeAllCache { result in
print(result ? "清理成功" : "清理失败")
}

}
}
92 changes: 52 additions & 40 deletions Source/RequestManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class RequestManager {
static let `default` = RequestManager()
private var requestTasks = [String: RequestTaskManager]()
private var timeoutIntervalForRequest: TimeInterval? /// 超时时间

func timeoutIntervalForRequest(_ timeInterval: TimeInterval) {
timeoutIntervalForRequest = timeInterval
RequestManager.default.timeoutIntervalForRequest = timeoutIntervalForRequest
}

func request(
_ url: String,
method: HTTPMethod = .get,
Expand All @@ -41,7 +41,7 @@ class RequestManager {
} else {
taskManager = requestTasks[key]
}

taskManager?.completionClosure = {
self.requestTasks.removeValue(forKey: key)
}
Expand All @@ -53,7 +53,7 @@ class RequestManager {
taskManager?.request(url, method: method, params: tempParam, cacheKey: key, encoding: encoding, headers: headers)
return taskManager!
}

func request(
urlRequest: URLRequestConvertible,
params: Parameters,
Expand All @@ -75,7 +75,7 @@ class RequestManager {
} else {
taskManager = requestTasks[key]
}

taskManager?.completionClosure = {
self.requestTasks.removeValue(forKey: key)
}
Expand All @@ -91,19 +91,19 @@ class RequestManager {
}
return nil
}

/// 取消请求
func cancel(_ url: String, params: Parameters? = nil, dynamicParams: Parameters? = nil) {
let key = cacheKey(url, params, dynamicParams)
let taskManager = requestTasks[key]
taskManager?.dataRequest?.cancel()
}

/// 清除所有缓存
func removeAllCache(completion: @escaping (Bool)->()) {
CacheManager.default.removeAllCache(completion: completion)
}

/// 根据key值清除缓存
func removeObjectCache(_ url: String, params: [String: Any]? = nil, dynamicParams: Parameters? = nil, completion: @escaping (Bool)->()) {
let key = cacheKey(url, params, dynamicParams)
Expand All @@ -119,7 +119,7 @@ public class RequestTaskManager {
fileprivate var cacheKey: String!
fileprivate var sessionManager: Session?
fileprivate var completionClosure: (()->())?

@discardableResult
fileprivate func timeoutIntervalForRequest(_ timeInterval: TimeInterval)->RequestTaskManager {
let configuration = URLSessionConfiguration.default
Expand All @@ -128,7 +128,7 @@ public class RequestTaskManager {
self.sessionManager = sessionManager
return self
}

@discardableResult
fileprivate func request(
_ url: String,
Expand All @@ -145,10 +145,10 @@ public class RequestTaskManager {
} else {
dataRequest = AF.request(url, method: method, parameters: params, encoding: encoding, headers: headers)
}

return self
}

/// request
///
/// - Parameters:
Expand All @@ -169,7 +169,13 @@ public class RequestTaskManager {
}
return self
}


/// 缓存data是否存在
public func cacheDataIsExist()->Bool {
let data = CacheManager.default.objectSync(forKey: cacheKey)?.data
return data != nil
}

/// 是否缓存数据
public func cache(_ cache: Bool)->RequestTaskManager {
self.cache = cache
Expand All @@ -178,9 +184,9 @@ public class RequestTaskManager {

/// 获取缓存Data
@discardableResult
public func cacheData(completion: @escaping (Data)->())->DaisyDataResponse {
public func cacheData()->Data? {
let dataResponse = DaisyDataResponse(dataRequest: dataRequest!, cache: cache, cacheKey: cacheKey, completionClosure: completionClosure)
return dataResponse.cacheData(completion: completion)
return dataResponse.cacheData()
}

/// 响应Data
Expand All @@ -197,9 +203,9 @@ public class RequestTaskManager {

/// 获取缓存String
@discardableResult
public func cacheString(completion: @escaping (String)->())->DaisyStringResponse {
public func cacheString()->String? {
let stringResponse = DaisyStringResponse(dataRequest: dataRequest!, cache: cache, cacheKey: cacheKey, completionClosure: completionClosure)
return stringResponse.cacheString(completion: completion)
return stringResponse.cacheString()
}

/// 响应String
Expand All @@ -216,9 +222,9 @@ public class RequestTaskManager {

/// 获取缓存JSON
@discardableResult
public func cacheJson(completion: @escaping (Any)->())->DaisyJsonResponse {
public func cacheJson()->Any? {
let jsonResponse = DaisyJsonResponse(dataRequest: dataRequest!, cache: cache, cacheKey: cacheKey, completionClosure: completionClosure)
return jsonResponse.cacheJson(completion: completion)
return jsonResponse.cacheJson()
}

/// 响应JSON
Expand Down Expand Up @@ -299,18 +305,20 @@ public class DaisyJsonResponse: DaisyResponse {
}

fileprivate func responseCacheAndJson(completion: @escaping (DaisyValue<Any>)->()) {
if cache { cacheJson(completion: { json in
let res = DaisyValue(isCacheData: true, result: Alamofire.AFResult.success(json), response: nil)
completion(res)
}) }
if cache {
if let json = cacheJson() {
let res = DaisyValue(isCacheData: true, result: Alamofire.AFResult.success(json), response: nil)
completion(res)
}
}
dataRequest.responseJSON { response in
self.responseCache(response: response, completion: completion)
}
}

/// 获取缓存json
@discardableResult
fileprivate func cacheJson(completion: @escaping (Any)->())->DaisyJsonResponse {
fileprivate func cacheJson()->Any? {
if let data = CacheManager.default.objectSync(forKey: cacheKey)?.data,
let json = try? JSONSerialization.jsonObject(with: data, options: [])
{
Expand All @@ -320,13 +328,13 @@ public class DaisyJsonResponse: DaisyResponse {
DaisyLog(str)
}
}
completion(json)
return json
} else {
if openResultLog {
DaisyLog("读取缓存失败")
}
return nil
}
return self
}
}

Expand All @@ -341,24 +349,26 @@ public class DaisyStringResponse: DaisyResponse {
}

@discardableResult
fileprivate func cacheString(completion: @escaping (String)->())->DaisyStringResponse {
fileprivate func cacheString()->String? {
if let data = CacheManager.default.objectSync(forKey: cacheKey)?.data,
let str = String(data: data, encoding: .utf8)
{
completion(str)
return str
} else {
if openResultLog {
DaisyLog("读取缓存失败")
}
return nil
}
return self
}

fileprivate func responseCacheAndString(completion: @escaping (DaisyValue<String>)->()) {
if cache { cacheString(completion: { str in
let res = DaisyValue(isCacheData: true, result: Alamofire.AFResult.success(str), response: nil)
completion(res)
}) }
if cache {
if let str = cacheString() {
let res = DaisyValue(isCacheData: true, result: Alamofire.AFResult.success(str), response: nil)
completion(res)
}
}
dataRequest.responseString { response in
self.responseCache(response: response, completion: completion)
}
Expand All @@ -376,22 +386,24 @@ public class DaisyDataResponse: DaisyResponse {
}

@discardableResult
fileprivate func cacheData(completion: @escaping (Data)->())->DaisyDataResponse {
fileprivate func cacheData()->Data? {
if let data = CacheManager.default.objectSync(forKey: cacheKey)?.data {
completion(data)
return data
} else {
if openResultLog {
DaisyLog("读取缓存失败")
}
return nil
}
return self
}

fileprivate func responseCacheAndData(completion: @escaping (DaisyValue<Data>)->()) {
if cache { cacheData(completion: { data in
let res = DaisyValue(isCacheData: true, result: Alamofire.AFResult.success(data), response: nil)
completion(res)
}) }
if cache {
if let data = cacheData() {
let res = DaisyValue(isCacheData: true, result: Alamofire.AFResult.success(data), response: nil)
completion(res)
}
}
dataRequest.responseData { response in
self.responseCache(response: response, completion: completion)
}
Expand Down

0 comments on commit f15a902

Please sign in to comment.