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 callbacksimport redis.dispatcher// Executing a non-blocking command and registering callbacks on the returned Futureredis.hGetAll("my-hash") onComplete { case Success(content) => println(content) case Failure(e) => e.printStackTrace()}// Executes a blocking command using the internal, lazily initialized BlockingClientredis.blocking.blPop(0, "queue")// Subscribes to a Pub/Sub channel using the internal, lazily initialized SubscriberClientredis.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 ActorSystemredis.quit()
评论