Scredis 是一个 Redis 的 Scala 客户端开发包。基于 Akka 构建,特点是 Reactive、非堵塞以及超级快。
示例代码:
import scredis._ import scala.util.{ Success, Failure } // Creates a Redis instance with default configuration. // See reference.conf for the complete list of configurable parameters. val redis = Redis() // Import internal ActorSystem's dispatcher (execution context) to register callbacks import redis.dispatcher // Executing a non-blocking command and registering callbacks on the returned Future redis.hGetAll("my-hash") onComplete { case Success(content) => println(content) case Failure(e) => e.printStackTrace() } // Executes a blocking command using the internal, lazily initialized BlockingClient redis.blocking.blPop(0, "queue") // Subscribes to a Pub/Sub channel using the internal, lazily initialized SubscriberClient redis.subscriber.subscribe("My Channel") { case message @ PubSubMessage.Message(channel, messageBytes) => println( message.readAs[String]() ) case PubSubMessage.Subscribe(channel, subscribedChannelsCount) => println( s"Successfully subscribed to $channel" ) } // Shutdown all initialized internal clients along with the ActorSystem redis.quit()