Skip to content

Command options 1.x.x

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

Almost every single command can be configured with an implicit CommandOptions object.

implicit opts: CommandOptions = DefaultCommandOptions

The latter provides four parameters.

final case class CommandOptions(
  // Command execution timeout, can be infinite (no timeout)
  timeout: Duration = 2 seconds,
  // Number of tries to perform in case unexpected errors occur
  tries: Int = 1,
  // Duration of time to sleep between tries (if more than one)
  sleep: Option[FiniteDuration] = Some(500 milliseconds),
  // When automatic pipelining is enabled, setting force to true "forces" the
  // command to be immediately executed
  force: Boolean = false
)

This allows you to have fine-grained control over execution of commands.

import scredis._
import scala.concurrent.duration._

val client = Client()

// Sets a 10ms timeout on this command
val str = client.get("STR")(CommandOptions(timeout = 10 millisecond))

// This is a critical command which we want to retry on any unexpected failure,
// e.g. if Redis server hangs for a bit
client.set("STR", "THIS IS CRITICAL STUFF")(
  CommandOptions(
    timeout = 5 seconds,
    tries = 5,
    sleep = Some(1 second)
  )
)

// All my commands are critical
implicit val opts = CommandOptions(
  timeout = 5 seconds,
  tries = 5,
  sleep = Some(1 second)
)

// The opts value will be used here because it has been implicitly defined above
client.set("STR", "SOME OTHER CRITICAL STUFF")
// Implicit opts value will be overwritten by DefaultCommandOptions here
client.set("STR", "I DON'T MIND LOSING THIS")(DefaultCommandOptions)