@@ -36,7 +36,7 @@ private let loggingKeyID = "RedisConnection"
36
36
/// A `RedisClient` implementation that represents an individual connection
37
37
/// to a Redis database instance.
38
38
///
39
- /// `RedisConnection` comes with logging and a method for creating `RedisPipeline` instances .
39
+ /// `RedisConnection` comes with logging by default .
40
40
///
41
41
/// See `RedisClient`
42
42
public final class RedisConnection : RedisClient {
@@ -49,10 +49,23 @@ public final class RedisConnection: RedisClient {
49
49
public var eventLoop : EventLoop { return channel. eventLoop }
50
50
/// Is the client still connected to Redis?
51
51
public var isConnected : Bool { return state != . closed }
52
+ /// Controls the timing behavior of sending commands over this connection. The default is `true`.
53
+ ///
54
+ /// When set to `false`, the host will "queue" commands and determine when to send all at once,
55
+ /// while `true` will force each command to be sent as soon as they are "queued".
56
+ /// - Note: Setting this to `true` will trigger all "queued" commands to be sent.
57
+ public var sendCommandsImmediately : Bool {
58
+ get { return autoflush. load ( ) }
59
+ set ( newValue) {
60
+ if newValue { channel. flush ( ) }
61
+ autoflush. store ( newValue)
62
+ }
63
+ }
52
64
53
65
private let channel : Channel
54
66
private var logger : Logger
55
67
68
+ private let autoflush = Atomic < Bool > ( value: true )
56
69
private let _stateLock = Lock ( )
57
70
private var _state : ConnectionState
58
71
private var state : ConnectionState {
@@ -112,19 +125,12 @@ public final class RedisConnection: RedisClient {
112
125
return result
113
126
}
114
127
115
- /// Creates a `RedisPipeline` for executing a batch of commands.
116
- /// - Note: The instance is given a `Logger` with the metadata property "RedisConnection"
117
- /// that contains the unique ID of the `RedisConnection` that created it.
128
+ /// Sends commands to the Redis instance this connection is tied to.
118
129
///
119
- /// - Returns: An `EventLoopFuture` resolving the `RedisPipeline` instance.
120
- public func makePipeline( ) -> RedisPipeline {
121
- var logger = Logger ( label: " NIORedis.RedisPipeline " )
122
- logger [ metadataKey: loggingKeyID] = self . logger [ metadataKey: loggingKeyID]
123
-
124
- return RedisPipeline ( channel: channel, logger: logger)
125
- }
126
-
127
130
/// See `RedisClient.send(command:with:)`
131
+ ///
132
+ /// - Note: The timing of when commands are actually sent to Redis are controlled by
133
+ /// the `sendCommandsImmediately` property.
128
134
public func send(
129
135
command: String ,
130
136
with arguments: [ RESPValueConvertible ]
@@ -148,8 +154,9 @@ public final class RedisConnection: RedisClient {
148
154
}
149
155
logger. debug ( " Sending command \" \( command) \" with \( arguments) encoded as \( args) " )
150
156
151
- _ = channel. writeAndFlush ( context)
152
-
153
- return promise. futureResult
157
+ guard sendCommandsImmediately else {
158
+ return channel. write ( context) . flatMap { promise. futureResult }
159
+ }
160
+ return channel. writeAndFlush ( context) . flatMap { promise. futureResult }
154
161
}
155
162
}
0 commit comments