Skip to content

Commit

Permalink
setCache
Browse files Browse the repository at this point in the history
  • Loading branch information
mengqingzheng committed Jun 22, 2024
1 parent 49625da commit e65f7bc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 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.1.0"
s.version = "1.1.1"
s.summary = "Alamofire与Cache封装, 更容易存储请求数据"
s.homepage = "https://github.com/MQZHot/DaisyNet"
s.license = "MIT"
Expand Down
36 changes: 32 additions & 4 deletions Source/DaisyNet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,35 @@ public class DaisyNet: NSObject {

public extension DaisyNet {
/// 缓存Data
static func cacheData(with identifier: String) -> Data? {
static func setCacheData(with data: Data, identifier: String?) {
if let cacheKey = CacheKey.with(identifier) {
let model = CacheModel(data: data)
CacheManager.default.setObject(model, forKey: cacheKey)
}
}

/// 缓存String
static func setCacheString(with string: String, identifier: String?) {
if let cacheKey = CacheKey.with(identifier),
let data = string.data(using: .utf8)
{
let model = CacheModel(data: data)
CacheManager.default.setObject(model, forKey: cacheKey)
}
}

/// 获取缓存json
static func setCacheJson(with json: Any, identifier: String?) {
if let cacheKey = CacheKey.with(identifier),
let data = try? JSONSerialization.data(withJSONObject: json)
{
let model = CacheModel(data: data)
CacheManager.default.setObject(model, forKey: cacheKey)
}
}

/// 获取缓存Data
static func cacheData(with identifier: String?) -> Data? {
if let cacheKey = CacheKey.with(identifier),
let data = CacheManager.default.objectSync(forKey: cacheKey)?.data
{
Expand All @@ -53,8 +81,8 @@ public extension DaisyNet {
return nil
}

/// 缓存String
static func cacheString(with identifier: String) -> String? {
/// 获取缓存String
static func cacheString(with identifier: String?) -> String? {
if let cacheKey = CacheKey.with(identifier),
let data = CacheManager.default.objectSync(forKey: cacheKey)?.data,
let str = String(data: data, encoding: .utf8)
Expand All @@ -65,7 +93,7 @@ public extension DaisyNet {
}

/// 获取缓存json
static func cacheJson(with identifier: String) -> Any? {
static func cacheJson(with identifier: String?) -> Any? {
if let cacheKey = CacheKey.with(identifier),
let data = CacheManager.default.objectSync(forKey: cacheKey)?.data,
let json = try? JSONSerialization.jsonObject(with: data, options: [])
Expand Down

0 comments on commit e65f7bc

Please sign in to comment.