Skip to content

Pipelining 1.x.x

Alexandre Curreli edited this page Jul 28, 2014 · 1 revision

Pipelining consists in sending several commands at the same time to the server, reducing the number of round trips and improving overall performance. There are several ways to pipeline commands. The first one is to call pipeline() on a Client and queue commands.

import scredis._

val client = Client()
// Creates a pipeline client used to queue up commands
val p = client.pipeline()
val set = p.set("STR", "Hello World!") // Future[Unit]
val get = p.get("STR") // Future[Option[String]]

// Triggers all commands to be sent together and completes the futures.
// The sync() method also returns an indexed sequence containing the results in the same
// order they were queued. If errors occur, the respective futures will fail and the
// returned result will be Failure(e).
val results = p.sync() // IndexedSeq(Success(Unit), Success(Some("Hello World!")))

The preferred method though is to use one of the helper functions below.

import scredis._

val client = Client()

// The sync() method automatically gets called at the end of the block.
val results = client.pipelined { p =>
  p.set("STR", "Hello World!")
  p.get("STR")
} // IndexedSeq(Success(Unit), Success(Some("Hello World!")))

// The pipelined1 variant allows to directly get the result of the returned
// Future (1 stands for a single result).
val str = client.pipelined1 { p =>
  p.set("STR", "Hello World!")
  p.get("STR")
} // Some("Hello World!")

// The pipelinedN variant allows to directly get the result of the returned
// Traversable of Future (N stands for a collection of results).
val list = client.pipelinedN { p =>
  List(p.set("STR", "Hello World!"), p.get("STR"))
} // List(Unit, Some("Hello World!"))