This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public typealias GenericRequestWithCacheResponse<T1: Codable, E1: Error> = AnyPublisher<T1, E1> | |
static func genericRequestWithCache<T1: Codable, E1: Error, T2: Codable, E2: Error>( | |
_ publisher: AnyPublisher<T2, E2>, | |
_ type: T1.Type, | |
_ cachePolicy: CachePolicy, | |
_ serviceKey: String, | |
_ serviceParams: [String], | |
_ cacheManager: CodableCacheManagerProtocol = SimpleCacheManagerForCodable.shared) -> GenericRequestWithCacheResponse<T1, E1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"status":"success","data":[{"id":1,"employee_name":"Tiger Nixon","employee_salary":320800,"employee_age":61,"profile_image":""},{"id":2,"employee_name":"Garrett Winters","employee_salary":170750,"employee_age":63,"profile_image":""},{"id":3,"employee_name":"Ashton Cox","employee_salary":86000,"employee_age":66,"profile_image":""},{"id":4,"employee_name":"Cedric Kelly","employee_salary":433060,"employee_age":22,"profile_image":""},{"id":5,"employee_name":"Airi Satou","employee_salary":162700,"employee_age":33,"profile_image":""},{"id":6,"employee_name":"Brielle Williamson","employee_salary":372000,"employee_age":61,"profile_image":""},{"id":7,"employee_name":"Herrod Chandler","employee_salary":137500,"employee_age":59,"profile_image":""},{"id":8,"employee_name":"Rhona Davidson","employee_salary":327900,"employee_age":55,"profile_image":""},{"id":9,"employee_name":"Colleen Hurst","employee_salary":205500,"employee_age":39,"profile_image":""},{"id":10,"employee_name":"Sonya Frost","employee_salary":103600,"empl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func syncRetrieve<T: Codable>(_ some: T.Type, key: String, params: [String]) -> (model: T, recordDate: Date)? { | |
var cachedRecords: [ExpiringCodableObjectWithKey] { | |
let records: [CDataExpiringKeyValueEntety] = Self.nonSecureInstanceSync.fetch() | |
return records .compactMap { record in | |
let encoding = ExpiringCodableObjectWithKey.ValueEncoding(rawValue: Int(record.encoding)) ?? .dataPlain | |
let model = ExpiringCodableObjectWithKey(key: record.key!, | |
expireDate: record.expireDate!, | |
object: record.object!, | |
objectType: record.objectType!, | |
encoding: encoding) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public typealias GenericRequestWithCacheResponse<T1: Codable, E1: Error> = AnyPublisher<T1, E1> | |
public extension NetworkinNameSpace.NetworkingUtils { | |
static func genericRequestWithCache<T1: Codable, E1: Error, T2: Codable, E2: Error>( | |
_ publisher: AnyPublisher<T2, E2>, | |
_ type: T1.Type, | |
_ cachePolicy: CachePolicy, | |
_ serviceKey: String, | |
_ serviceParams: [String], | |
_ cacheManager: CodableCacheManagerProtocol = SimpleCacheManagerForCodable.shared) -> GenericRequestWithCacheResponse<T1, E1> { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SimpleCacheManagerForCodable: CodableCacheManagerProtocol { | |
private init() {} | |
public static let shared = SimpleCacheManagerForCodable() | |
public func syncStore<T: Codable>(_ codable: T, | |
key: String, | |
params: [String], | |
timeToLiveMinutes: Int? = nil) { | |
let expiringCodableObjectWithKey = ExpiringCodableObjectWithKey(codable, | |
key: key, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol CodableCacheManagerProtocol { | |
func syncStore<T: Codable>(_ codable: T, key: String, params: [String], timeToLiveMinutes: Int?) | |
func syncRetrieve<T: Codable>(_ type: T.Type, key: String, params: [String]) -> (model: T, recordDate: Date)? | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public extension ExpiringCodableObjectWithKey { | |
var isExpired: Bool { valueData == nil } | |
var valueData: Data? { | |
Self.referenceDate > expireDate ? object : nil | |
} | |
func extract<T: Codable>(_ some: T.Type) -> T? { | |
guard let data = valueData else { | |
return nil | |
} | |
return try? JSONDecoder().decode(T.self, from: data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias ResponseWithCacheMaybe = AnyPublisher<ResponseDto.EmployeeServiceAvailability, AppErrors> | |
/// No cache API call | |
public func requestWithoutCache(param: String) -> ResponseWithCacheMaybe { | |
let requestDto = FRPSampleAPI.RequestDto.Employee(someParam: param) | |
let apiRequest = sampleRepository2.requestSampleJSON(requestDto) | |
return apiRequest.mapError({$0.toAppError}).eraseToAnyPublisher() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExpiringCodableObjectWithKey: Codable { | |
var key: String! // Cache key (built using api request name and parameters) | |
var object: Data! // Value to be stored | |
var expireDate: Date! // The limit date in witch we can retried the object | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum CacheStrategy { | |
case cacheNoLoad | |
case noCacheLoad | |
case cacheElseLoad | |
case cacheAndLoad | |
} |
NewerOlder