Skip to content

Commit ba6bf6d

Browse files
authored
Merge pull request #26 from Mordil/redis-pipeline
Simplify `RedisPipeline`
2 parents 0f7b53f + c4ea8f9 commit ba6bf6d

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

Sources/NIORedis/RedisClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public final class RedisConnection: RedisClient {
106106
var logger = Logger(label: "NIORedis.RedisPipeline")
107107
logger[metadataKey: loggingKeyID] = self.logger[metadataKey: loggingKeyID]
108108

109-
return NIORedisPipeline(channel: channel, logger: logger)
109+
return RedisPipeline(channel: channel, logger: logger)
110110
}
111111

112112
/// See `RedisClient.send(command:with:)`

Sources/NIORedis/RedisPipeline.swift

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,12 @@ import Logging
1414
///
1515
/// See [https://redis.io/topics/pipelining#redis-pipelining](https://redis.io/topics/pipelining#redis-pipelining)
1616
/// - Important: The larger the pipeline queue, the more memory both NIORedis and Redis will use.
17-
public protocol RedisPipeline {
17+
///
18+
/// This implements `RedisClient` to use itself for enqueuing commands.
19+
public final class RedisPipeline {
1820
/// The number of commands in the pipeline.
19-
var count: Int { get }
20-
21-
/// Queues an operation executed with the provided `RedisClient` that will be executed in sequence when
22-
/// `execute()` is invoked.
23-
///
24-
/// let pipeline = connection.makePipeline()
25-
/// .enqueue { $0.set("my_key", "3") }
26-
/// .enqueue { $0.send(command: "INCR", with: ["my_key"]) }
27-
///
28-
/// See `RedisClient`.
29-
/// - Parameter operation: The operation specified with `RedisClient` provided.
30-
/// - Returns: A self-reference for chaining commands.
31-
@discardableResult
32-
func enqueue<T>(operation: (RedisClient) -> EventLoopFuture<T>) -> RedisPipeline
33-
34-
/// Flushes the queue, sending all of the commands to Redis.
35-
/// - Returns: An `EventLoopFuture` that resolves the `RESPValue` responses, in the same order as the command queue.
36-
func execute() -> EventLoopFuture<[RESPValue]>
37-
}
21+
public var count: Int { return queuedCommandResults.count }
3822

39-
public final class NIORedisPipeline {
4023
private var logger: Logger
4124
/// The channel being used to send commands with.
4225
private let channel: Channel
@@ -46,7 +29,7 @@ public final class NIORedisPipeline {
4629

4730
/// Creates a new pipeline queue that will write to the channel provided.
4831
/// - Parameter channel: The `Channel` to write to.
49-
public init(channel: Channel, logger: Logger = Logger(label: "NIORedis.Pipeline")) {
32+
public init(channel: Channel, logger: Logger = Logger(label: "NIORedis.RedisPipeline")) {
5033
self.channel = channel
5134
self.logger = logger
5235
self.queuedCommandResults = []
@@ -56,24 +39,33 @@ public final class NIORedisPipeline {
5639
}
5740
}
5841

59-
extension NIORedisPipeline: RedisPipeline {
60-
/// See `RedisPipeline.count`.
61-
public var count: Int {
62-
return queuedCommandResults.count
63-
}
42+
// MARK: Enqueue & Execute
6443

65-
/// See `RedisPipeline.enqueue(operation:)`.
44+
extension RedisPipeline {
45+
/// Queues an operation executed with the provided `RedisClient` that will be executed in
46+
/// sequence when `execute()` is invoked.
47+
///
48+
/// let pipeline = connection.makePipeline()
49+
/// .enqueue { $0.set("my_key", to: 3) }
50+
/// .enqueue { $0.increment("my_key") }
51+
///
52+
/// See `RedisClient`.
53+
/// - Parameter operation: The operation specified with the `RedisClient` provided.
54+
/// - Returns: A self-reference for chaining commands.
6655
@discardableResult
6756
public func enqueue<T>(operation: (RedisClient) -> EventLoopFuture<T>) -> RedisPipeline {
6857
// We are passing ourselves in as the executor instance,
69-
// and our implementation of `RedisCommandExecutor.send(command:with:) handles the actual queueing.
58+
// and our implementation of `RedisClient.send(command:with:) handles the actual queueing.
7059
_ = operation(self)
7160
logger.debug("Command queued. Pipeline size: \(count)")
7261
return self
7362
}
7463

75-
/// See `RedisPipeline.execute()`.
76-
/// - Important: If any of the commands fail, the remaining commands will not execute and the `EventLoopFuture` will fail.
64+
/// Flushes the queue, sending all of the commands to Redis.
65+
/// - Important: If any of the commands fail, the remaining commands will not execute
66+
/// and the `EventLoopFuture` will fail.
67+
/// - Returns: An `EventLoopFuture` that resolves the `RESPValue` responses,
68+
/// in the same order as the command queue.
7769
public func execute() -> EventLoopFuture<[RESPValue]> {
7870
let response = EventLoopFuture<[RESPValue]>.reduce(
7971
into: [],
@@ -97,14 +89,21 @@ extension NIORedisPipeline: RedisPipeline {
9789
}
9890
}
9991

100-
extension NIORedisPipeline: RedisClient {
101-
/// See `RedisCommandExecutor.eventLoop`.
102-
public var eventLoop: EventLoop { return self.channel.eventLoop }
92+
// MARK: RedisClient
93+
94+
extension RedisPipeline: RedisClient {
95+
/// See `RedisClient.eventLoop`
96+
public var eventLoop: EventLoop { return channel.eventLoop }
10397

10498
/// Sends the command and arguments to a buffer to later be flushed when `execute()` is invoked.
105-
/// - Note: When working with a `NIORedisPipeline` instance directly, it is preferred to use the
99+
/// - Note: When working with a `RedisPipeline` instance directly, it is preferred to use the
106100
/// `RedisPipeline.enqueue(operation:)` method instead of `send(command:with:)`.
107-
public func send(command: String, with arguments: [RESPValueConvertible] = []) -> EventLoopFuture<RESPValue> {
101+
///
102+
/// See `RedisClient.send(command:with:)`
103+
public func send(
104+
command: String,
105+
with arguments: [RESPValueConvertible] = []
106+
) -> EventLoopFuture<RESPValue> {
108107
let args = arguments.map { $0.convertedToRESPValue() }
109108

110109
let promise = channel.eventLoop.makePromise(of: RESPValue.self)

Tests/NIORedisTests/RedisPipelineTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class RedisPipelineTests: XCTestCase {
2222
try? redis.terminate()
2323
}
2424

25-
func test_enqueue() {
25+
func test_enqueue() throws {
2626
let pipeline = connection.makePipeline()
2727

2828
pipeline.enqueue { $0.send(command: "PING") }

0 commit comments

Comments
 (0)