use connection::{connect, Connection, ConnectionInfo, ConnectionLike, IntoConnectionInfo};
use types::{RedisFuture, RedisResult, Value};
#[derive(Debug, Clone)]
pub struct Client {
connection_info: ConnectionInfo,
}
impl Client {
pub fn open<T: IntoConnectionInfo>(params: T) -> RedisResult<Client> {
Ok(Client {
connection_info: try!(params.into_connection_info()),
})
}
pub fn get_connection(&self) -> RedisResult<Connection> {
Ok(try!(connect(&self.connection_info)))
}
pub fn get_async_connection(&self) -> RedisFuture<::async::Connection> {
::async::connect(self.connection_info.clone())
}
pub fn get_shared_async_connection(&self) -> RedisFuture<::async::SharedConnection> {
use futures::Future;
Box::new(
self.get_async_connection()
.and_then(move |con| ::async::SharedConnection::new(con)),
)
}
}
impl ConnectionLike for Client {
fn req_packed_command(&self, cmd: &[u8]) -> RedisResult<Value> {
try!(self.get_connection()).req_packed_command(cmd)
}
fn req_packed_commands(
&self,
cmd: &[u8],
offset: usize,
count: usize,
) -> RedisResult<Vec<Value>> {
try!(self.get_connection()).req_packed_commands(cmd, offset, count)
}
fn get_db(&self) -> i64 {
self.connection_info.db
}
}