mirror of
https://github.com/swift-server/RediStack.git
synced 2026-05-03 07:32:28 +00:00
badf342391f4559bfaa0dd6d7c07464eb4794273
NIORedis: A Redis Driver built on SwiftNIO
- Pitch discussion: Swift Server Forums
NOTE: This this is written against SwiftNIO 2.0, and as such requires Swift 5.0!
This is to take advantage of the Result type in the DispatchRedis module,
and to stay ahead of development of the next version of SwiftNIO.
import NIORedis
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let driver = RedisDriver(ownershipModel: .external(elg))
// connections
// passing a value to 'password' will automatically authenticate with Redis before resolving the connection
let connection = try redis.makeConnection(
hostname: "localhost", // this is the default
port: 6379, // this is the default
password: "MY_PASS" // default is 'nil'
).wait()
// convenience methods for commands
let result = try conneciton.set("my_key", to: "some value")
.then { return connection.get("my_key")}
.wait()
print(result) // Optional("some value")
// raw commands
let keyCount = try connection.command("DEL", [RESPValue(bulk: "my_key")])
.thenThrowing { response in
guard case let .integer(count) else {
// throw error
}
return count
}
.wait()
print(keyCount) // 1
// cleanup
connection.close()
.thenThrowing { try redis.terminate() }
.whenSuccess { try elg.syncShutdownGracefully() }
RESPValue & RESPValueConvertible
This is a 1:1 mapping enum of the RESP types: Simple String, Bulk String, Array, Integer and Error.
Conforming to RESPValueConvertible allows Swift types to more easily convert between RESPValue and native types.
Array, Data, Float, Double, FixedWidthInteger, String, and of course RESPValue all conform in this package.
Description
Languages
Swift
99.7%
Shell
0.3%