@@ -26,19 +26,19 @@ object RedisClient {
26
26
)
27
27
.getOrElse(0 )
28
28
}
29
+
30
+ case class CommandToSend (command : String , args : Seq [Array [Byte ]])
29
31
}
30
32
31
33
import RedisClient ._
32
34
abstract class Redis (batch : Mode ) extends IO with Protocol {
33
35
var handlers : Vector [(String , () => Any )] = Vector .empty
34
- var commandBuffer : StringBuffer = new StringBuffer
35
- val crlf = " \r\n "
36
-
36
+ val commandBuffer = collection.mutable.ListBuffer .empty[CommandToSend ]
37
37
38
38
def send [A ](command : String , args : Seq [Any ])(result : => A )(implicit format : Format ): A = try {
39
39
if (batch == BATCH ) {
40
40
handlers :+= ((command, () => result))
41
- commandBuffer.append(( List (command) ++ args.toList).mkString( " " ) ++ crlf )
41
+ commandBuffer += CommandToSend (command, args.map(format.apply) )
42
42
null .asInstanceOf [A ] // hack
43
43
} else {
44
44
write(Commands .multiBulk(command.getBytes(" UTF-8" ) +: (args map (format.apply))))
@@ -53,26 +53,36 @@ abstract class Redis(batch: Mode) extends IO with Protocol {
53
53
else throw e
54
54
}
55
55
56
- def send [A ](command : String , submissionMode : Boolean = false )(result : => A ): A = try {
56
+ def send [A ](command : String )(result : => A ): A = try {
57
57
if (batch == BATCH ) {
58
- if (! submissionMode) {
59
- handlers :+= ((command, () => result))
60
- commandBuffer.append(command ++ crlf)
61
- null .asInstanceOf [A ]
62
- } else {
63
- write(command.getBytes(" UTF-8" ))
64
- result
65
- }
58
+ handlers :+= ((command, () => result))
59
+ commandBuffer += CommandToSend (command, Seq .empty[Array [Byte ]])
60
+ null .asInstanceOf [A ]
66
61
} else {
67
62
write(Commands .multiBulk(List (command.getBytes(" UTF-8" ))))
68
63
result
69
64
}
70
65
} catch {
71
66
case e : RedisConnectionException =>
72
- if (disconnect) send(command, submissionMode)(result)
67
+ if (disconnect) send(command)(result)
68
+ else throw e
69
+ case e : SocketException =>
70
+ if (disconnect) send(command)(result)
71
+ else throw e
72
+ }
73
+
74
+ def send [A ](commands : List [CommandToSend ])(result : => A ): A = try {
75
+ val cs = commands.map { command =>
76
+ command.command.getBytes(" UTF-8" ) +: command.args
77
+ }
78
+ write(Commands .multiMultiBulk(cs))
79
+ result
80
+ } catch {
81
+ case e : RedisConnectionException =>
82
+ if (disconnect) send(commands)(result)
73
83
else throw e
74
84
case e : SocketException =>
75
- if (disconnect) send(command, submissionMode )(result)
85
+ if (disconnect) send(commands )(result)
76
86
else throw e
77
87
}
78
88
@@ -143,17 +153,17 @@ class RedisClient(override val host: String, override val port: Int,
143
153
* @see https://redis.io/commands/multi
144
154
*/
145
155
def pipeline (f : PipelineClient => Any ): Option [List [Any ]] = {
146
- send(" MULTI" , false )(asString) // flush reply stream
156
+ send(" MULTI" )(asString) // flush reply stream
147
157
try {
148
158
val pipelineClient = new PipelineClient (this )
149
159
try {
150
160
f(pipelineClient)
151
161
} catch {
152
162
case e : Exception =>
153
- send(" DISCARD" , false )(asString)
163
+ send(" DISCARD" )(asString)
154
164
throw e
155
165
}
156
- send(" EXEC" , false )(asExec(pipelineClient.responseHandlers))
166
+ send(" EXEC" )(asExec(pipelineClient.responseHandlers))
157
167
} catch {
158
168
case e : RedisMultiExecException =>
159
169
None
@@ -226,9 +236,9 @@ class RedisClient(override val host: String, override val port: Int,
226
236
commands.foreach { command =>
227
237
command()
228
238
}
229
- val r = send(commandBuffer.toString, true )(Some (handlers.map(_._2).map(_()).toList))
239
+ val r = send(commandBuffer.toList )(Some (handlers.map(_._2).map(_()).toList))
230
240
handlers = Vector .empty
231
- commandBuffer.setLength( 0 )
241
+ commandBuffer.clear( )
232
242
r
233
243
}
234
244
@@ -248,7 +258,7 @@ class RedisClient(override val host: String, override val port: Int,
248
258
receive(singleLineReply).map(Parse .parseDefault)
249
259
null .asInstanceOf [A ] // ugh... gotta find a better way
250
260
}
251
- override def send [A ](command : String , submissionMode : Boolean = false )(result : => A ): A = {
261
+ override def send [A ](command : String )(result : => A ): A = {
252
262
write(Commands .multiBulk(List (command.getBytes(" UTF-8" ))))
253
263
responseHandlers :+= (() => result)
254
264
receive(singleLineReply).map(Parse .parseDefault)
0 commit comments