@@ -26,19 +26,19 @@ object RedisClient {
2626 )
2727 .getOrElse(0 )
2828 }
29+
30+ case class CommandToSend (command : String , args : Seq [Array [Byte ]])
2931}
3032
3133import RedisClient ._
3234abstract class Redis (batch : Mode ) extends IO with Protocol {
3335 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 ]
3737
3838 def send [A ](command : String , args : Seq [Any ])(result : => A )(implicit format : Format ): A = try {
3939 if (batch == BATCH ) {
4040 handlers :+= ((command, () => result))
41- commandBuffer.append(( List (command) ++ args.toList).mkString( " " ) ++ crlf )
41+ commandBuffer += CommandToSend (command, args.map(format.apply) )
4242 null .asInstanceOf [A ] // hack
4343 } else {
4444 write(Commands .multiBulk(command.getBytes(" UTF-8" ) +: (args map (format.apply))))
@@ -53,26 +53,36 @@ abstract class Redis(batch: Mode) extends IO with Protocol {
5353 else throw e
5454 }
5555
56- def send [A ](command : String , submissionMode : Boolean = false )(result : => A ): A = try {
56+ def send [A ](command : String )(result : => A ): A = try {
5757 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 ]
6661 } else {
6762 write(Commands .multiBulk(List (command.getBytes(" UTF-8" ))))
6863 result
6964 }
7065 } catch {
7166 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)
7383 else throw e
7484 case e : SocketException =>
75- if (disconnect) send(command, submissionMode )(result)
85+ if (disconnect) send(commands )(result)
7686 else throw e
7787 }
7888
@@ -143,17 +153,17 @@ class RedisClient(override val host: String, override val port: Int,
143153 * @see https://redis.io/commands/multi
144154 */
145155 def pipeline (f : PipelineClient => Any ): Option [List [Any ]] = {
146- send(" MULTI" , false )(asString) // flush reply stream
156+ send(" MULTI" )(asString) // flush reply stream
147157 try {
148158 val pipelineClient = new PipelineClient (this )
149159 try {
150160 f(pipelineClient)
151161 } catch {
152162 case e : Exception =>
153- send(" DISCARD" , false )(asString)
163+ send(" DISCARD" )(asString)
154164 throw e
155165 }
156- send(" EXEC" , false )(asExec(pipelineClient.responseHandlers))
166+ send(" EXEC" )(asExec(pipelineClient.responseHandlers))
157167 } catch {
158168 case e : RedisMultiExecException =>
159169 None
@@ -226,9 +236,9 @@ class RedisClient(override val host: String, override val port: Int,
226236 commands.foreach { command =>
227237 command()
228238 }
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))
230240 handlers = Vector .empty
231- commandBuffer.setLength( 0 )
241+ commandBuffer.clear( )
232242 r
233243 }
234244
@@ -248,7 +258,7 @@ class RedisClient(override val host: String, override val port: Int,
248258 receive(singleLineReply).map(Parse .parseDefault)
249259 null .asInstanceOf [A ] // ugh... gotta find a better way
250260 }
251- override def send [A ](command : String , submissionMode : Boolean = false )(result : => A ): A = {
261+ override def send [A ](command : String )(result : => A ): A = {
252262 write(Commands .multiBulk(List (command.getBytes(" UTF-8" ))))
253263 responseHandlers :+= (() => result)
254264 receive(singleLineReply).map(Parse .parseDefault)
0 commit comments