Skip to content
Alexandre Curreli edited this page Jul 31, 2014 · 7 revisions

The Client class represents a non-blocking Redis client.

Note: the default Client cannot be used to issue blocking requests or to subscribe to channels/patterns.

Initialization

import scredis._
import com.typesafe.config.ConfigFactory
import akka.actor.ActorSystem

// Defines an actor system to use with the client
implicit val system = ActorSystem("my-actor-system")

// Creates a non-blocking client with default configuration (see reference.conf)
val client = Client()

// Creates a non-blocking client with provided parameters, falling back to default
// configuration for non-provided parameters.
val clientWithParameters = Client(
  host = "192.168.1.1",
  port = 6380,
  passwordOpt = Some("foobar")
)

// Explicitly load a config instance
val config = ConfigFactory.load("scredis.conf").getConfig("scredis")
// Creates a non-blocking client with specified config, falling back to default configuration
val clientWithExplicitConfig = Client(config)

// Creates a non-blocking client with specified config file name and path,
// falling back to default configuration
val clientWithExplicitConfigFileNameAndPath = Client("scredis.conf", "scredis")

Full usage example

import scredis._
import akka.actor.ActorSystem
import scala.util.{ Success, Failure }

// Defines an actor system to use with the client
implicit val system = ActorSystem("my-actor-system")

// Creates a non-blocking client with default configuration (see reference.conf)
val client = Client()
// Import the ActorSystem's dispatcher (execution context) to be able to register callbacks
import client.dispatcher

// Send a GET request and register a callback
client.get("foo").onComplete {
  case Success(Some(value)) => // do something with the value
  case Success(None) => // there is no value associated to key 'foo'
  case Failure(e) => // an error occurred while processing the request
}
Clone this wiki locally