Skip to content

Commit 5cd86f6

Browse files
authored
ValkeyClient.pipeline (#104)
1 parent 9161960 commit 5cd86f6

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Sources/Valkey/ValkeyClient.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,26 @@ extension ValkeyClient: ValkeyConnectionProtocol {
169169
}
170170
}
171171

172+
extension ValkeyClient {
173+
/// Pipeline a series of commands to Valkey connection
174+
///
175+
/// This function will only return once it has the results of all the commands sent
176+
/// - Parameter commands: Parameter pack of ValkeyCommands
177+
/// - Returns: Parameter pack holding the results of all the commands
178+
@inlinable
179+
public func pipeline<each Command: ValkeyCommand>(
180+
_ commands: repeat each Command
181+
) async -> sending (repeat Result<(each Command).Response, Error>) {
182+
do {
183+
return try await self.withConnection { connection in
184+
await connection.pipeline(repeat (each commands))
185+
}
186+
} catch {
187+
return (repeat Result<(each Command).Response, Error>.failure(error))
188+
}
189+
}
190+
}
191+
172192
#if ServiceLifecycleSupport
173193
extension ValkeyClient: Service {}
174194
#endif // ServiceLifecycle

Tests/IntegrationTests/ValkeyTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ struct GeneratedCommands {
165165
}
166166
}
167167

168+
@Test
169+
func testPipelinedSetGetClient() async throws {
170+
var logger = Logger(label: "Valkey")
171+
logger.logLevel = .debug
172+
try await withValkeyClient(.hostname(valkeyHostname, port: 6379), logger: logger) { client in
173+
try await withKey(connection: client) { key in
174+
let responses = await client.pipeline(
175+
SET(key: key, value: "Pipelined Hello"),
176+
GET(key: key)
177+
)
178+
try #expect(responses.1.get()?.decode(as: String.self) == "Pipelined Hello")
179+
}
180+
}
181+
}
182+
168183
@Test
169184
func testTransactionSetIncrGet() async throws {
170185
var logger = Logger(label: "Valkey")

0 commit comments

Comments
 (0)