|
1 | | -# Redis |
| 1 | +# Valkey |
2 | 2 |
|
3 | | -Redis client. |
| 3 | +Valkey client. |
4 | 4 |
|
5 | 5 | This is currently in an unfinished state. It has no connection manager so there is no support for connection reuse. |
6 | 6 |
|
7 | 7 | ## Usage |
8 | 8 |
|
9 | | -To create a connection to Redis database you use `RedisClient.withConnection()`. |
| 9 | +To create a connection to Valkey database you use `ValkeyClient.withConnection()`. |
10 | 10 |
|
11 | 11 | ```swift |
12 | | -try await RedisClient.withConnection(.hostname("localhost", port: 6379), logger: logger) { connection, logger in |
13 | | - try await doRedisStuff() |
| 12 | +try await ValkeyClient.withConnection(.hostname("localhost", port: 6379), logger: logger) { connection, logger in |
| 13 | + try await doValkeyStuff() |
14 | 14 | } |
15 | 15 | ``` |
16 | 16 |
|
17 | | -Swift-redis has three ways to send commands. |
18 | | - |
19 | | -### Raw |
20 | | - |
21 | | -You can send raw commands using `RedisConnection.send()`. This takes a parameter pack of types that conform to `RESPRenderable`. These includes `Int`, `String`, `Double`, `RedisKey` and then `Optional` and `Array` where the internal type is also `RedisRenderable`. For example to set a value you can call |
22 | | - |
23 | | -```swift |
24 | | -let key = RedisKey(rawValue: "MyKey") |
25 | | -try await connection.send("SET", key, "TestString") |
26 | | -``` |
27 | | - |
28 | | -A raw send will return a `RESPToken`. This can be converted to any value that conforms to `RESPTokenRepresentable`. As with `RESPRenderable` these covers many standard types. You can convert from a `RESPToken` to `RESPTokenRepresentable` type by either using the constructor `init(from: RESPToken)` or the function `converting(to:)`. |
29 | | - |
30 | | -```swift |
31 | | -let response = try await connection.send("GET", key) |
32 | | -let value = try response.converting(to: String.self) |
33 | | -// you could also use `let value = String(from: response)` |
34 | | -``` |
35 | | - |
36 | | -### Using generated functions |
37 | | - |
38 | | -Swift-redis includes a separate module `RedisCommands` that includes functions for all the redis commands. These are generated from the model files redis supplies in [redis-doc](https://github.com/redis/redis-doc). Instead of searching up in the documentation how a command is structured. You can just call a Swift function. In many cases where it is possible the return type from these functions is the set to the be the expected type. In situations where the type cannot be ascertained a `RESPToken` is returned and you'll need to convert the return type manually. |
39 | | - |
40 | | -With the generated functions the code above becomes |
| 17 | +All the Valkey commands are in the Commands folder of the Valkey target. These are generated from the model files Valkey supplies in [valkey-doc](https://github.com/valkey-io/valkey-doc). In many cases where it was possible to ascertain the return type of a command these functions will return that expected type. In situations where this is not possible a `RESPToken` is returned and you'll need to convert the return type manually. |
41 | 18 |
|
42 | 19 | ```swift |
43 | | -let key = RedisKey(rawValue: "MyKey") |
44 | | -try await connection.set(key, "TestString") |
| 20 | +try await connection.set("MyKey", "TestString") |
45 | 21 | let value = try await connection.get(key) |
46 | 22 | ``` |
47 | 23 |
|
48 | | -### Using generated RESPCommands and pipelining |
| 24 | +### Pipelining commands |
49 | 25 |
|
50 | | -In general you don't need to use this method as it has no advantages over using the generated functions. But `RESPCommands` has a function for each Redis command. Where the generated `RESPCommands` are useful is if you want to pipeline commands ie send multiple commands batched together and only wait for all of their responses in one place. You could pipeline the two commands above using |
| 26 | +In some cases it desirable to send multiple commands at one time, without waiting for each response after each command. This is called pipelining. You can do this using the function `pipeline(_:)`. This takes a parameter pack of commands and returns a parameter pack with the responses once all the commands have executed. |
51 | 27 |
|
52 | 28 | ```swift |
53 | | -let key = RedisKey(rawValue: "MyKey") |
54 | 29 | let (setResponse, getResponse) = try await connection.pipeline( |
55 | | - SET(key, "TestString"), |
56 | | - GET(key) |
| 30 | + SET("MyKey", "TestString"), |
| 31 | + GET("MyKey") |
57 | 32 | ) |
58 | 33 | ``` |
| 34 | + |
| 35 | +## Redis compatibilty |
| 36 | + |
| 37 | +As Valkey is a fork of Redis v7.2.4, swift-valkey is compatible with Redis databases up to v7.2.4. There is a chance the v7.2.4 features will still be compatible in later versions of Redis, but these are now considered two different projects and they will diverge. Swift-valkey uses the RESP3 protocol. It does not support RESP2. |
0 commit comments