Skip to content
Clone
dd4650a
79 lines 2.5 KB Rust
use std::io;
use deadpool_redis::PoolError;
use deadpool_redis::redis::aio::ConnectionLike;
use deadpool_redis::redis::{Cmd, Pipeline, RedisError, RedisFuture, Value};
use crate::kvcache::conn::{CacheConn, CacheConnection};
use crate::kvcache::error::CacheError;
impl ConnectionLike for CacheConn {
fn req_packed_command<'a>(&'a mut self, cmd: &'a Cmd) -> RedisFuture<'a, Value> {
Box::pin(async move {
let mut connection = self.acquire().await.map_err(connect_error)?;
connection.req_packed_command(cmd).await
})
}
fn req_packed_commands<'a>(
&'a mut self,
pipeline: &'a Pipeline,
offset: usize,
count: usize,
) -> RedisFuture<'a, Vec<Value>> {
Box::pin(async move {
let mut connection = self.acquire().await.map_err(connect_error)?;
connection
.req_packed_commands(pipeline, offset, count)
.await
})
}
fn get_db(&self) -> i64 {
self.database()
}
}
impl ConnectionLike for CacheConnection {
fn req_packed_command<'a>(&'a mut self, cmd: &'a Cmd) -> RedisFuture<'a, Value> {
match self {
Self::Standard(connection) => connection.req_packed_command(cmd),
Self::Cluster(connection) => connection.req_packed_command(cmd),
}
}
fn req_packed_commands<'a>(
&'a mut self,
pipeline: &'a Pipeline,
offset: usize,
count: usize,
) -> RedisFuture<'a, Vec<Value>> {
match self {
Self::Standard(connection) => connection.req_packed_commands(pipeline, offset, count),
Self::Cluster(connection) => connection.req_packed_commands(pipeline, offset, count),
}
}
fn get_db(&self) -> i64 {
match self {
Self::Standard(connection) => connection.get_db(),
Self::Cluster(connection) => connection.get_db(),
}
}
}
fn connect_error(error: CacheError) -> RedisError {
match error {
CacheError::Pool(error) => pool_error(error),
error => io::Error::other(error).into(),
}
}
pub(crate) fn pool_error(error: PoolError) -> RedisError {
match error {
PoolError::Backend(error) => error,
error @ PoolError::Timeout(_) => io::Error::new(io::ErrorKind::TimedOut, error).into(),
error @ PoolError::Closed => io::Error::new(io::ErrorKind::NotConnected, error).into(),
error => io::Error::other(error).into(),
}
}