PSSRedisClient 0.1.17

PSSRedisClient 0.1.17

测试已测试
语言语言 SwiftSwift
许可 MIT
发布上一次发布2019年6月
SPM支持 SPM

Eric Silverberg 维护。



PSSRedisClient

一个简单的基于 SwiftRedis 接口,使用 CocoaAsyncSocket

介绍

我们有一个项目需要现代的 ObjC 或 Swift-based redis 客户端实现。经过一些研究,我们发现大多数 objc 或 swift 库要么尝试同时实现套接字通信和 redis 协议,要么使用单一的套接字通信库。例如,以下是一个这样的双套接字和 redis 协议实现,ObjCHiredis。这样的库包含大量的网络和指针逻辑,我们特别发现这个库容易崩溃。另外,经验表明,指针过多的 C/C++ 代码如果不积极维护,几乎肯定会存在漏洞。

为了最大限度地提高对我们套接字通信的可靠性信心,我们调查了 iOS 套接字库。出于以下原因,我们选择了 CocoaAsyncSocket

  1. iOS 套接字库 的 Google 搜索结果的首选
  2. 它在公有领域
  3. 超过 8k 星和 2k 分支,这比 下一个最受欢迎的库 多了一个数量级
  4. 活跃且经常维护

最后,鉴于我们对 redis 客户端的额外考虑,我们希望这个库可以通过 Cocoapods 安装。

存在许多基于 Swift 的 redis 接口,包括

  1. RedBird - 这个库依赖于 Vaport Socks 库而不是 CocoaAsyncSocket 库的网络功能。它不在 Cocoapods 上。
  2. Zewo Redis - 这个库依赖于Zewo自己的 TCP套接字库,再次不是 CocoaAsyncSocket 库。它不在Cocoapods上可用。
  3. Swidis - 这个库似乎没有实际的代码。它不在Cocoapods上可用。
  4. SwiftRedis - 这个库似乎自己实现了网络功能,而不是使用 CocoaAsyncSocket 库。它不在Cocoapods上可用。

由于这些解决方案都不是基于CocoaAsyncSockets构建的,并且都不在Cocoapods上可用,因此我们创建了一个自己的简单类,该类能够使用CocoaAsyncSockets进行网络组件,并解析 Redis协议

示例

要运行示例项目,首先克隆存储库,然后从示例目录运行 pod install

要求

iOS 9+

安装

PSSRedisClient 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的Podfile中

pod "PSSRedisClient"

用法

override func viewDidLoad() {
    ... 

    self.redisManager = RedisClient(delegate: self)
    self.subscriptionManager = RedisClient(delegate: self)
    
    self.redisManager?.connect(host: "localhost",
                               port: 6379,
                               pwd: "password")
    self.subscriptionManager?.connect(host: "localhost",
                                      port: 6379,
                                      pwd: "password")
}

func socketDidConnect(client: RedisClient) {
    debugPrint("SOCKET: Connected")

    // Setup a subscription after we have connected
    if (redisManager == self.subscriptionManager) {
        self.subscriptionManager?.exec(args: ["subscribe", channel], completion: nil)
    }
}

func socketDidDisconnect(client: RedisClient, error: Error?) {
    debugPrint("Disconnected (Error: \(error?.localizedDescription))")
}

func subscriptionMessageReceived(results: NSArray) {
    if (results.count == 3 && results.firstObject as? String != nil) {
        let message = results.firstObject as! String

        if (message == "message") {
            debugPrint("SOCKET: Sending message of \(results[2])");

            self.results.text = "Subscription heard: \(results[2])"
        } else if (message == "subscribe") {
            debugPrint("SOCKET: Subscription successful");
        } else {
            debugPrint("SOCKET: Unknown message received");
        }
    }
}

func messageReceived(message: NSArray) {
    if (message.firstObject as? NSError != nil) {
        let error = message.firstObject as! NSError
        let userInfo = error.userInfo

        if let possibleMessage = userInfo["message"] {
            if let actualMessage = possibleMessage as? String {
                debugPrint("SOCKETS: Error: \(actualMessage)")
            }
        }
    } else {
        debugPrint("Results: \(message.componentsJoined(by: " "))")
    }
}

作者

Eric Silverberg, @esilverberg

许可证

PSSRedisClient 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。