redis/client.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
use std::time::Duration;
use crate::{
connection::{connect, Connection, ConnectionInfo, ConnectionLike, IntoConnectionInfo},
types::{RedisResult, Value},
};
#[cfg(feature = "aio")]
use std::pin::Pin;
#[cfg(feature = "tls-rustls")]
use crate::tls::{inner_build_with_tls, TlsCertificates};
/// The client type.
#[derive(Debug, Clone)]
pub struct Client {
pub(crate) connection_info: ConnectionInfo,
}
/// The client acts as connector to the redis server. By itself it does not
/// do much other than providing a convenient way to fetch a connection from
/// it. In the future the plan is to provide a connection pool in the client.
///
/// When opening a client a URL in the following format should be used:
///
/// ```plain
/// redis://host:port/db
/// ```
///
/// Example usage::
///
/// ```rust,no_run
/// let client = redis::Client::open("redis://127.0.0.1/").unwrap();
/// let con = client.get_connection().unwrap();
/// ```
impl Client {
/// Connects to a redis server and returns a client. This does not
/// actually open a connection yet but it does perform some basic
/// checks on the URL that might make the operation fail.
pub fn open<T: IntoConnectionInfo>(params: T) -> RedisResult<Client> {
Ok(Client {
connection_info: params.into_connection_info()?,
})
}
/// Instructs the client to actually connect to redis and returns a
/// connection object. The connection object can be used to send
/// commands to the server. This can fail with a variety of errors
/// (like unreachable host) so it's important that you handle those
/// errors.
pub fn get_connection(&self) -> RedisResult<Connection> {
connect(&self.connection_info, None)
}
/// Instructs the client to actually connect to redis with specified
/// timeout and returns a connection object. The connection object
/// can be used to send commands to the server. This can fail with
/// a variety of errors (like unreachable host) so it's important
/// that you handle those errors.
pub fn get_connection_with_timeout(&self, timeout: Duration) -> RedisResult<Connection> {
connect(&self.connection_info, Some(timeout))
}
/// Returns a reference of client connection info object.
pub fn get_connection_info(&self) -> &ConnectionInfo {
&self.connection_info
}
}
/// To enable async support you need to chose one of the supported runtimes and active its
/// corresponding feature: `tokio-comp` or `async-std-comp`
#[cfg(feature = "aio")]
#[cfg_attr(docsrs, doc(cfg(feature = "aio")))]
impl Client {
/// Returns an async connection from the client.
#[cfg(any(feature = "tokio-comp", feature = "async-std-comp"))]
#[deprecated(
note = "aio::Connection is deprecated. Use client::get_multiplexed_async_connection instead."
)]
#[allow(deprecated)]
pub async fn get_async_connection(&self) -> RedisResult<crate::aio::Connection> {
let con = match Runtime::locate() {
#[cfg(feature = "tokio-comp")]
Runtime::Tokio => {
self.get_simple_async_connection::<crate::aio::tokio::Tokio>()
.await?
}
#[cfg(feature = "async-std-comp")]
Runtime::AsyncStd => {
self.get_simple_async_connection::<crate::aio::async_std::AsyncStd>()
.await?
}
};
crate::aio::Connection::new(&self.connection_info.redis, con).await
}
/// Returns an async connection from the client.
#[cfg(feature = "tokio-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-comp")))]
#[deprecated(
note = "aio::Connection is deprecated. Use client::get_multiplexed_tokio_connection instead."
)]
#[allow(deprecated)]
pub async fn get_tokio_connection(&self) -> RedisResult<crate::aio::Connection> {
use crate::aio::RedisRuntime;
Ok(
crate::aio::connect::<crate::aio::tokio::Tokio>(&self.connection_info)
.await?
.map(RedisRuntime::boxed),
)
}
/// Returns an async connection from the client.
#[cfg(feature = "async-std-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std-comp")))]
#[deprecated(
note = "aio::Connection is deprecated. Use client::get_multiplexed_async_std_connection instead."
)]
#[allow(deprecated)]
pub async fn get_async_std_connection(&self) -> RedisResult<crate::aio::Connection> {
use crate::aio::RedisRuntime;
Ok(
crate::aio::connect::<crate::aio::async_std::AsyncStd>(&self.connection_info)
.await?
.map(RedisRuntime::boxed),
)
}
/// Returns an async connection from the client.
#[cfg(any(feature = "tokio-comp", feature = "async-std-comp"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "tokio-comp", feature = "async-std-comp")))
)]
pub async fn get_multiplexed_async_connection(
&self,
) -> RedisResult<crate::aio::MultiplexedConnection> {
self.get_multiplexed_async_connection_with_timeouts(
std::time::Duration::MAX,
std::time::Duration::MAX,
)
.await
}
/// Returns an async connection from the client.
#[cfg(any(feature = "tokio-comp", feature = "async-std-comp"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "tokio-comp", feature = "async-std-comp")))
)]
pub async fn get_multiplexed_async_connection_with_timeouts(
&self,
response_timeout: std::time::Duration,
connection_timeout: std::time::Duration,
) -> RedisResult<crate::aio::MultiplexedConnection> {
let result = match Runtime::locate() {
#[cfg(feature = "tokio-comp")]
rt @ Runtime::Tokio => {
rt.timeout(
connection_timeout,
self.get_multiplexed_async_connection_inner::<crate::aio::tokio::Tokio>(
response_timeout,
),
)
.await
}
#[cfg(feature = "async-std-comp")]
rt @ Runtime::AsyncStd => {
rt.timeout(
connection_timeout,
self.get_multiplexed_async_connection_inner::<crate::aio::async_std::AsyncStd>(
response_timeout,
),
)
.await
}
};
match result {
Ok(Ok(connection)) => Ok(connection),
Ok(Err(e)) => Err(e),
Err(elapsed) => Err(elapsed.into()),
}
}
/// Returns an async multiplexed connection from the client.
///
/// A multiplexed connection can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
#[cfg(feature = "tokio-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-comp")))]
pub async fn get_multiplexed_tokio_connection_with_response_timeouts(
&self,
response_timeout: std::time::Duration,
connection_timeout: std::time::Duration,
) -> RedisResult<crate::aio::MultiplexedConnection> {
let result = Runtime::locate()
.timeout(
connection_timeout,
self.get_multiplexed_async_connection_inner::<crate::aio::tokio::Tokio>(
response_timeout,
),
)
.await;
match result {
Ok(Ok(connection)) => Ok(connection),
Ok(Err(e)) => Err(e),
Err(elapsed) => Err(elapsed.into()),
}
}
/// Returns an async multiplexed connection from the client.
///
/// A multiplexed connection can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
#[cfg(feature = "tokio-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-comp")))]
pub async fn get_multiplexed_tokio_connection(
&self,
) -> RedisResult<crate::aio::MultiplexedConnection> {
self.get_multiplexed_tokio_connection_with_response_timeouts(
std::time::Duration::MAX,
std::time::Duration::MAX,
)
.await
}
/// Returns an async multiplexed connection from the client.
///
/// A multiplexed connection can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
#[cfg(feature = "async-std-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std-comp")))]
pub async fn get_multiplexed_async_std_connection_with_timeouts(
&self,
response_timeout: std::time::Duration,
connection_timeout: std::time::Duration,
) -> RedisResult<crate::aio::MultiplexedConnection> {
let result = Runtime::locate()
.timeout(
connection_timeout,
self.get_multiplexed_async_connection_inner::<crate::aio::async_std::AsyncStd>(
response_timeout,
),
)
.await;
match result {
Ok(Ok(connection)) => Ok(connection),
Ok(Err(e)) => Err(e),
Err(elapsed) => Err(elapsed.into()),
}
}
/// Returns an async multiplexed connection from the client.
///
/// A multiplexed connection can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
#[cfg(feature = "async-std-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std-comp")))]
pub async fn get_multiplexed_async_std_connection(
&self,
) -> RedisResult<crate::aio::MultiplexedConnection> {
self.get_multiplexed_async_std_connection_with_timeouts(
std::time::Duration::MAX,
std::time::Duration::MAX,
)
.await
}
/// Returns an async multiplexed connection from the client and a future which must be polled
/// to drive any requests submitted to it (see `get_multiplexed_tokio_connection`).
///
/// A multiplexed connection can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
/// The multiplexer will return a timeout error on any request that takes longer then `response_timeout`.
#[cfg(feature = "tokio-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-comp")))]
pub async fn create_multiplexed_tokio_connection_with_response_timeout(
&self,
response_timeout: std::time::Duration,
) -> RedisResult<(
crate::aio::MultiplexedConnection,
impl std::future::Future<Output = ()>,
)> {
self.create_multiplexed_async_connection_inner::<crate::aio::tokio::Tokio>(response_timeout)
.await
}
/// Returns an async multiplexed connection from the client and a future which must be polled
/// to drive any requests submitted to it (see `get_multiplexed_tokio_connection`).
///
/// A multiplexed connection can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
#[cfg(feature = "tokio-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-comp")))]
pub async fn create_multiplexed_tokio_connection(
&self,
) -> RedisResult<(
crate::aio::MultiplexedConnection,
impl std::future::Future<Output = ()>,
)> {
self.create_multiplexed_tokio_connection_with_response_timeout(std::time::Duration::MAX)
.await
}
/// Returns an async multiplexed connection from the client and a future which must be polled
/// to drive any requests submitted to it (see `get_multiplexed_tokio_connection`).
///
/// A multiplexed connection can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
/// The multiplexer will return a timeout error on any request that takes longer then `response_timeout`.
#[cfg(feature = "async-std-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std-comp")))]
pub async fn create_multiplexed_async_std_connection_with_response_timeout(
&self,
response_timeout: std::time::Duration,
) -> RedisResult<(
crate::aio::MultiplexedConnection,
impl std::future::Future<Output = ()>,
)> {
self.create_multiplexed_async_connection_inner::<crate::aio::async_std::AsyncStd>(
response_timeout,
)
.await
}
/// Returns an async multiplexed connection from the client and a future which must be polled
/// to drive any requests submitted to it (see `get_multiplexed_tokio_connection`).
///
/// A multiplexed connection can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
#[cfg(feature = "async-std-comp")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std-comp")))]
pub async fn create_multiplexed_async_std_connection(
&self,
) -> RedisResult<(
crate::aio::MultiplexedConnection,
impl std::future::Future<Output = ()>,
)> {
self.create_multiplexed_async_std_connection_with_response_timeout(std::time::Duration::MAX)
.await
}
/// Returns an async [`ConnectionManager`][connection-manager] from the client.
///
/// The connection manager wraps a
/// [`MultiplexedConnection`][multiplexed-connection]. If a command to that
/// connection fails with a connection error, then a new connection is
/// established in the background and the error is returned to the caller.
///
/// This means that on connection loss at least one command will fail, but
/// the connection will be re-established automatically if possible. Please
/// refer to the [`ConnectionManager`][connection-manager] docs for
/// detailed reconnecting behavior.
///
/// A connection manager can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
///
/// [connection-manager]: aio/struct.ConnectionManager.html
/// [multiplexed-connection]: aio/struct.MultiplexedConnection.html
#[cfg(feature = "connection-manager")]
#[cfg_attr(docsrs, doc(cfg(feature = "connection-manager")))]
#[deprecated(note = "use get_connection_manager instead")]
pub async fn get_tokio_connection_manager(&self) -> RedisResult<crate::aio::ConnectionManager> {
crate::aio::ConnectionManager::new(self.clone()).await
}
/// Returns an async [`ConnectionManager`][connection-manager] from the client.
///
/// The connection manager wraps a
/// [`MultiplexedConnection`][multiplexed-connection]. If a command to that
/// connection fails with a connection error, then a new connection is
/// established in the background and the error is returned to the caller.
///
/// This means that on connection loss at least one command will fail, but
/// the connection will be re-established automatically if possible. Please
/// refer to the [`ConnectionManager`][connection-manager] docs for
/// detailed reconnecting behavior.
///
/// A connection manager can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
///
/// [connection-manager]: aio/struct.ConnectionManager.html
/// [multiplexed-connection]: aio/struct.MultiplexedConnection.html
#[cfg(feature = "connection-manager")]
#[cfg_attr(docsrs, doc(cfg(feature = "connection-manager")))]
pub async fn get_connection_manager(&self) -> RedisResult<crate::aio::ConnectionManager> {
crate::aio::ConnectionManager::new(self.clone()).await
}
/// Returns an async [`ConnectionManager`][connection-manager] from the client.
///
/// The connection manager wraps a
/// [`MultiplexedConnection`][multiplexed-connection]. If a command to that
/// connection fails with a connection error, then a new connection is
/// established in the background and the error is returned to the caller.
///
/// This means that on connection loss at least one command will fail, but
/// the connection will be re-established automatically if possible. Please
/// refer to the [`ConnectionManager`][connection-manager] docs for
/// detailed reconnecting behavior.
///
/// A connection manager can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
///
/// [connection-manager]: aio/struct.ConnectionManager.html
/// [multiplexed-connection]: aio/struct.MultiplexedConnection.html
#[cfg(feature = "connection-manager")]
#[cfg_attr(docsrs, doc(cfg(feature = "connection-manager")))]
#[deprecated(note = "use get_connection_manager_with_backoff instead")]
pub async fn get_tokio_connection_manager_with_backoff(
&self,
exponent_base: u64,
factor: u64,
number_of_retries: usize,
) -> RedisResult<crate::aio::ConnectionManager> {
self.get_tokio_connection_manager_with_backoff_and_timeouts(
exponent_base,
factor,
number_of_retries,
std::time::Duration::MAX,
std::time::Duration::MAX,
)
.await
}
/// Returns an async [`ConnectionManager`][connection-manager] from the client.
///
/// The connection manager wraps a
/// [`MultiplexedConnection`][multiplexed-connection]. If a command to that
/// connection fails with a connection error, then a new connection is
/// established in the background and the error is returned to the caller.
///
/// This means that on connection loss at least one command will fail, but
/// the connection will be re-established automatically if possible. Please
/// refer to the [`ConnectionManager`][connection-manager] docs for
/// detailed reconnecting behavior.
///
/// A connection manager can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
///
/// [connection-manager]: aio/struct.ConnectionManager.html
/// [multiplexed-connection]: aio/struct.MultiplexedConnection.html
#[cfg(feature = "connection-manager")]
#[cfg_attr(docsrs, doc(cfg(feature = "connection-manager")))]
pub async fn get_tokio_connection_manager_with_backoff_and_timeouts(
&self,
exponent_base: u64,
factor: u64,
number_of_retries: usize,
response_timeout: std::time::Duration,
connection_timeout: std::time::Duration,
) -> RedisResult<crate::aio::ConnectionManager> {
crate::aio::ConnectionManager::new_with_backoff_and_timeouts(
self.clone(),
exponent_base,
factor,
number_of_retries,
response_timeout,
connection_timeout,
)
.await
}
/// Returns an async [`ConnectionManager`][connection-manager] from the client.
///
/// The connection manager wraps a
/// [`MultiplexedConnection`][multiplexed-connection]. If a command to that
/// connection fails with a connection error, then a new connection is
/// established in the background and the error is returned to the caller.
///
/// This means that on connection loss at least one command will fail, but
/// the connection will be re-established automatically if possible. Please
/// refer to the [`ConnectionManager`][connection-manager] docs for
/// detailed reconnecting behavior.
///
/// A connection manager can be cloned, allowing requests to be be sent concurrently
/// on the same underlying connection (tcp/unix socket).
///
/// [connection-manager]: aio/struct.ConnectionManager.html
/// [multiplexed-connection]: aio/struct.MultiplexedConnection.html
#[cfg(feature = "connection-manager")]
#[cfg_attr(docsrs, doc(cfg(feature = "connection-manager")))]
pub async fn get_connection_manager_with_backoff(
&self,
exponent_base: u64,
factor: u64,
number_of_retries: usize,
) -> RedisResult<crate::aio::ConnectionManager> {
crate::aio::ConnectionManager::new_with_backoff(
self.clone(),
exponent_base,
factor,
number_of_retries,
)
.await
}
async fn get_multiplexed_async_connection_inner<T>(
&self,
response_timeout: std::time::Duration,
) -> RedisResult<crate::aio::MultiplexedConnection>
where
T: crate::aio::RedisRuntime,
{
let (connection, driver) = self
.create_multiplexed_async_connection_inner::<T>(response_timeout)
.await?;
T::spawn(driver);
Ok(connection)
}
async fn create_multiplexed_async_connection_inner<T>(
&self,
response_timeout: std::time::Duration,
) -> RedisResult<(
crate::aio::MultiplexedConnection,
impl std::future::Future<Output = ()>,
)>
where
T: crate::aio::RedisRuntime,
{
let con = self.get_simple_async_connection::<T>().await?;
crate::aio::MultiplexedConnection::new_with_response_timeout(
&self.connection_info.redis,
con,
response_timeout,
)
.await
}
async fn get_simple_async_connection<T>(
&self,
) -> RedisResult<Pin<Box<dyn crate::aio::AsyncStream + Send + Sync>>>
where
T: crate::aio::RedisRuntime,
{
Ok(crate::aio::connect_simple::<T>(&self.connection_info)
.await?
.boxed())
}
#[cfg(feature = "connection-manager")]
pub(crate) fn connection_info(&self) -> &ConnectionInfo {
&self.connection_info
}
/// Constructs a new `Client` with parameters necessary to create a TLS connection.
///
/// - `conn_info` - URL using the `rediss://` scheme.
/// - `tls_certs` - `TlsCertificates` structure containing:
/// -- `client_tls` - Optional `ClientTlsConfig` containing byte streams for
/// --- `client_cert` - client's byte stream containing client certificate in PEM format
/// --- `client_key` - client's byte stream containing private key in PEM format
/// -- `root_cert` - Optional byte stream yielding PEM formatted file for root certificates.
///
/// If `ClientTlsConfig` ( cert+key pair ) is not provided, then client-side authentication is not enabled.
/// If `root_cert` is not provided, then system root certificates are used instead.
///
/// # Examples
///
/// ```no_run
/// use std::{fs::File, io::{BufReader, Read}};
///
/// use redis::{Client, AsyncCommands as _, TlsCertificates, ClientTlsConfig};
///
/// async fn do_redis_code(
/// url: &str,
/// root_cert_file: &str,
/// cert_file: &str,
/// key_file: &str
/// ) -> redis::RedisResult<()> {
/// let root_cert_file = File::open(root_cert_file).expect("cannot open private cert file");
/// let mut root_cert_vec = Vec::new();
/// BufReader::new(root_cert_file)
/// .read_to_end(&mut root_cert_vec)
/// .expect("Unable to read ROOT cert file");
///
/// let cert_file = File::open(cert_file).expect("cannot open private cert file");
/// let mut client_cert_vec = Vec::new();
/// BufReader::new(cert_file)
/// .read_to_end(&mut client_cert_vec)
/// .expect("Unable to read client cert file");
///
/// let key_file = File::open(key_file).expect("cannot open private key file");
/// let mut client_key_vec = Vec::new();
/// BufReader::new(key_file)
/// .read_to_end(&mut client_key_vec)
/// .expect("Unable to read client key file");
///
/// let client = Client::build_with_tls(
/// url,
/// TlsCertificates {
/// client_tls: Some(ClientTlsConfig{
/// client_cert: client_cert_vec,
/// client_key: client_key_vec,
/// }),
/// root_cert: Some(root_cert_vec),
/// }
/// )
/// .expect("Unable to build client");
///
/// let connection_info = client.get_connection_info();
///
/// println!(">>> connection info: {connection_info:?}");
///
/// let mut con = client.get_async_connection().await?;
///
/// con.set("key1", b"foo").await?;
///
/// redis::cmd("SET")
/// .arg(&["key2", "bar"])
/// .query_async(&mut con)
/// .await?;
///
/// let result = redis::cmd("MGET")
/// .arg(&["key1", "key2"])
/// .query_async(&mut con)
/// .await;
/// assert_eq!(result, Ok(("foo".to_string(), b"bar".to_vec())));
/// println!("Result from MGET: {result:?}");
///
/// Ok(())
/// }
/// ```
#[cfg(feature = "tls-rustls")]
pub fn build_with_tls<C: IntoConnectionInfo>(
conn_info: C,
tls_certs: TlsCertificates,
) -> RedisResult<Client> {
let connection_info = conn_info.into_connection_info()?;
inner_build_with_tls(connection_info, tls_certs)
}
/// Returns an async receiver for pub-sub messages.
#[cfg(any(feature = "tokio-comp", feature = "async-std-comp"))]
// TODO - do we want to type-erase pubsub using a trait, to allow us to replace it with a different implementation later?
pub async fn get_async_pubsub(&self) -> RedisResult<crate::aio::PubSub> {
#[allow(deprecated)]
self.get_async_connection()
.await
.map(|connection| connection.into_pubsub())
}
/// Returns an async receiver for monitor messages.
#[cfg(any(feature = "tokio-comp", feature = "async-std-comp"))]
// TODO - do we want to type-erase monitor using a trait, to allow us to replace it with a different implementation later?
pub async fn get_async_monitor(&self) -> RedisResult<crate::aio::Monitor> {
#[allow(deprecated)]
self.get_async_connection()
.await
.map(|connection| connection.into_monitor())
}
}
#[cfg(feature = "aio")]
use crate::aio::Runtime;
impl ConnectionLike for Client {
fn req_packed_command(&mut self, cmd: &[u8]) -> RedisResult<Value> {
self.get_connection()?.req_packed_command(cmd)
}
fn req_packed_commands(
&mut self,
cmd: &[u8],
offset: usize,
count: usize,
) -> RedisResult<Vec<Value>> {
self.get_connection()?
.req_packed_commands(cmd, offset, count)
}
fn get_db(&self) -> i64 {
self.connection_info.redis.db
}
fn check_connection(&mut self) -> bool {
if let Ok(mut conn) = self.get_connection() {
conn.check_connection()
} else {
false
}
}
fn is_open(&self) -> bool {
if let Ok(conn) = self.get_connection() {
conn.is_open()
} else {
false
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn regression_293_parse_ipv6_with_interface() {
assert!(Client::open(("fe80::cafe:beef%eno1", 6379)).is_ok());
}
}