-
Notifications
You must be signed in to change notification settings - Fork 16
Transactions 1.x.x
Alexandre Curreli edited this page Jul 28, 2014
·
1 revision
Transactions are used similarly to pipelining. You can create a TransactionalClient and queue some commands.
import scredis._
val client = Client()
// Creates a transactional client used to queue up commands
val m = client.multi()
val set = m.set("STR", "Hello World!") // Future[Unit]
val get = m.get("STR") // Future[Option[String]]
// Triggers all commands to be sent together as part of a Redis transaction and
// completes the futures. The exec() method also returns an indexed sequence containing
// the results in the same order they were queued. If errors occur, the transaction is
// aborted, the respective futures will fail and the returned result will be Failure(e).
val results = m.exec() // 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 exec() method automatically gets called at the end of the block.
val results = client.transactional { m =>
m.set("STR", "Hello World!")
m.get("STR")
} // IndexedSeq(Success(Unit), Success(Some("Hello World!")))
// The transactional1 variant allows to directly get the result of the returned
// Future (1 stands for a single result).
val str = client.transactional1 { m =>
m.set("STR", "Hello World!")
m.get("STR")
} // Some("Hello World!")
// The transactionalN variant allows to directly get the result of the returned
// Traversable of Future (N stands for a collection of results).
val list = client.transactionalN { m =>
List(m.set("STR", "Hello World!"), m.get("STR"))
} // List(Unit, Some("Hello World!"))