|
|
@ -11,21 +11,20 @@ public extension LokiAPI {
|
|
|
|
private static let swarmCacheKey = "swarmCacheKey"
|
|
|
|
private static let swarmCacheKey = "swarmCacheKey"
|
|
|
|
private static let swarmCacheCollection = "swarmCacheCollection"
|
|
|
|
private static let swarmCacheCollection = "swarmCacheCollection"
|
|
|
|
|
|
|
|
|
|
|
|
fileprivate static var swarmCache: [String:[Target]] = [:]
|
|
|
|
fileprivate static var swarmCache: [String:[Target]] {
|
|
|
|
|
|
|
|
get {
|
|
|
|
@objc public static func loadSwarmCache() {
|
|
|
|
var result: [String:[Target]]? = nil
|
|
|
|
var result: [String:[Target]]? = nil
|
|
|
|
storage.dbReadConnection.read { transaction in
|
|
|
|
storage.dbReadConnection.read { transaction in
|
|
|
|
let intermediate = transaction.object(forKey: swarmCacheKey, inCollection: swarmCacheCollection) as! [String:[TargetWrapper]]?
|
|
|
|
let intermediate = transaction.object(forKey: swarmCacheKey, inCollection: swarmCacheCollection) as! [String:[TargetWrapper]]?
|
|
|
|
result = intermediate?.mapValues { $0.map { Target(from: $0) } }
|
|
|
|
result = intermediate?.mapValues { $0.map { Target(from: $0) } }
|
|
|
|
}
|
|
|
|
|
|
|
|
return result ?? [:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
swarmCache = result ?? [:]
|
|
|
|
set {
|
|
|
|
}
|
|
|
|
let intermediate = newValue.mapValues { $0.map { TargetWrapper(from: $0) } }
|
|
|
|
|
|
|
|
storage.dbReadWriteConnection.readWrite { transaction in
|
|
|
|
private static func saveSwarmCache() {
|
|
|
|
transaction.setObject(intermediate, forKey: swarmCacheKey, inCollection: swarmCacheCollection)
|
|
|
|
let intermediate = swarmCache.mapValues { $0.map { TargetWrapper(from: $0) } }
|
|
|
|
}
|
|
|
|
storage.dbReadWriteConnection.readWrite { transaction in
|
|
|
|
|
|
|
|
transaction.setObject(intermediate, forKey: swarmCacheKey, inCollection: swarmCacheCollection)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -41,10 +40,7 @@ public extension LokiAPI {
|
|
|
|
return Promise<[Target]> { $0.fulfill(cachedSwarm) }
|
|
|
|
return Promise<[Target]> { $0.fulfill(cachedSwarm) }
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
let parameters: [String:Any] = [ "pubKey" : hexEncodedPublicKey ]
|
|
|
|
let parameters: [String:Any] = [ "pubKey" : hexEncodedPublicKey ]
|
|
|
|
return getRandomSnode().then { invoke(.getSwarm, on: $0, associatedWith: hexEncodedPublicKey, parameters: parameters) }.map { parseTargets(from: $0) }.get { swarm in
|
|
|
|
return getRandomSnode().then { invoke(.getSwarm, on: $0, associatedWith: hexEncodedPublicKey, parameters: parameters) }.map { parseTargets(from: $0) }.get { swarmCache[hexEncodedPublicKey] = $0 }
|
|
|
|
swarmCache[hexEncodedPublicKey] = swarm
|
|
|
|
|
|
|
|
saveSwarmCache()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|