[go: up one dir, main page]

Skip to content

Alamofire与Cache封装 , 更容易存储请求数据

License

Notifications You must be signed in to change notification settings

MQZHot/DaisyNet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DaisyNet

AlamofireCache的封装实现对网络数据的缓存,可以存储JSON,String,Data.

使用

  • 缓存数据需要调用.cacheIdentifier(identifier),不调用或者.cacheIdentifier(nil)不缓存
  • 确保identifier是唯一的
/// 缓存标识
let identifier = "home"

/// 网络请求
DaisyNet.request(urlStr, params: params).cacheIdentifier(identifier).responseString(queue: .main) { result in
    switch result {
    case .success(let string):
        self.textView.text = string
        print(Thread.current)
    case .failure(let error):
        print(error)
    }
}
  • 读取缓存
/// 缓存标识
let identifier = "home"

/// 读取缓存
let cacheString = DaisyNet.cacheString(with: identifier)
let cacheData = DaisyNet.cacheData(with: identifier)
let cacheJson = DaisyNet.cacheJson(with: identifier)
  • 缓存过期时间
    func cacheExpiryConfig(expiry: DaisyExpiry)
  • 清除缓存
/// 清除所有缓存
func removeAllCache(completion: ((_ isSuccess: Bool) -> ())? = nil) 
/// 根据identifier清除缓存
func removeCache(with identifier: String?, completion: ((_ isSuccess: Bool) -> ())? = nil)

Install

1.pod 'DaisyNet'

2.pod install / pod update

Author

LICENSE

DaisyNet is released under the MIT license. See LICENSE for details.