Client
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.
- To send blocking requests, use BlockingClient
- To subscribe to channels/patterns, use SubscriberClient
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,
authOpt = Some(AuthConfig(username = Some("userX"), password = "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
}
The source code for this page can be found here.