|
1 |
| -# NIORedis: A Redis Driver built on SwiftNIO |
| 1 | +[](https://www.apache.org/licenses/LICENSE-2.0.html) |
| 2 | +[](https://circleci.com/gh/Mordil/nio-redis/tree/master) |
| 3 | +[](https://swift.org) |
2 | 4 |
|
3 |
| -* Pitch discussion: [Swift Server Forums](https://forums.swift.org/t/swiftnio-redis-client/19325/13) |
| 5 | +# NIORedis |
4 | 6 |
|
5 |
| -> **NOTE: This this is written against SwiftNIO 2.0, and as such requires Swift 5.0!** |
| 7 | +* Pitch discussion: [Swift Server Forums](https://forums.swift.org/t/swiftnio-redis-client/19325) |
| 8 | +* Proposal: [SSWG-0004](https://github.com/swift-server/sswg/blob/56a26b50ade45d624b54abe13c7d1f88526f9bb1/proposals/0004-nio-redis.md) |
6 | 9 |
|
7 |
| -This is to take advantage of the [`Result`](https://github.com/apple/swift-evolution/blob/master/proposals/0235-add-result.md) type in the `DispatchRedis` module, |
8 |
| -and to stay ahead of development of the next version of SwiftNIO. |
| 10 | +## Installation |
| 11 | + |
| 12 | +To install `NIORedis`, just add the package as a dependency in your [**Package.swift**](https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4.md#dependencies) |
9 | 13 |
|
10 | 14 | ```swift
|
11 |
| -import NIORedis |
| 15 | +dependencies: [ |
| 16 | + .package(url: "https://github.com/Mordil/nio-redis.git", .upToNextMinor(from: "0.2.0") |
| 17 | +] |
| 18 | +``` |
12 | 19 |
|
13 |
| -let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
14 |
| -let driver = RedisDriver(ownershipModel: .external(elg)) |
| 20 | +and run the following command: `swift package resolve` |
15 | 21 |
|
16 |
| -// connections |
| 22 | +## Getting Started |
17 | 23 |
|
18 |
| -// passing a value to 'password' will automatically authenticate with Redis before resolving the connection |
19 |
| -let connection = try redis.makeConnection( |
20 |
| - hostname: "localhost", // this is the default |
21 |
| - port: 6379, // this is the default |
22 |
| - password: "MY_PASS" // default is 'nil' |
23 |
| -).wait() |
| 24 | +`NIORedis` is ready to use right after installation. |
24 | 25 |
|
25 |
| -// convenience methods for commands |
| 26 | +```swift |
| 27 | +import NIORedis |
26 | 28 |
|
27 |
| -let result = try conneciton.set("my_key", to: "some value") |
28 |
| - .then { return connection.get("my_key")} |
29 |
| - .wait() |
30 |
| -print(result) // Optional("some value") |
| 29 | +let driver = NIORedisDriver(ownershipModel: .internal(threadCount: 2)) |
31 | 30 |
|
32 |
| -// raw commands |
| 31 | +let connection = try driver.makeConnection().wait() |
33 | 32 |
|
34 |
| -let keyCount = try connection.command("DEL", [RESPValue(bulk: "my_key")]) |
35 |
| - .thenThrowing { response in |
36 |
| - guard case let .integer(count) else { |
37 |
| - // throw error |
38 |
| - } |
39 |
| - return count |
40 |
| - } |
| 33 | +let result = try connection.set("my_key", to: "some value") |
| 34 | + .flatMap { return connection.get("my_key" } |
41 | 35 | .wait()
|
42 |
| -print(keyCount) // 1 |
43 |
| - |
44 |
| -// cleanup |
45 | 36 |
|
46 |
| -connection.close() |
47 |
| - .thenThrowing { try redis.terminate() } |
48 |
| - .whenSuccess { try elg.syncShutdownGracefully() } |
| 37 | +print(result) // Optional("some value") |
49 | 38 | ```
|
50 |
| - |
51 |
| -### RESPValue & RESPValueConvertible |
52 |
| -This is a 1:1 mapping enum of the `RESP` types: `Simple String`, `Bulk String`, `Array`, `Integer` and `Error`. |
53 |
| - |
54 |
| -Conforming to `RESPValueConvertible` allows Swift types to more easily convert between `RESPValue` and native types. |
55 |
| - |
56 |
| -`Array`, `Data`, `Float`, `Double`, `FixedWidthInteger`, `String`, and of course `RESPValue` all conform in this package. |
0 commit comments