tower_redis/
service.rs

1use redis::{
2    aio::{ConnectionLike, ConnectionManager},
3    Cmd, RedisError, RedisFuture, Value,
4};
5use std::task::{Context, Poll};
6
7/// A Tower service for asynchronous Redis request/response performed over a
8/// managed, multplexed connection.
9#[derive(Clone)]
10pub struct RedisService {
11    /// Redis connection for queries
12    inner: ConnectionManager,
13}
14
15impl RedisService {
16    // Create a new service with the provided connection manager
17    pub fn new(inner: ConnectionManager) -> Self {
18        Self { inner }
19    }
20}
21
22impl tower::Service<Cmd> for RedisService {
23    type Response = Value;
24    type Error = RedisError;
25    type Future = RedisFuture<'static, Value>;
26
27    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
28        Poll::Ready(Ok(()))
29    }
30
31    fn call(&mut self, req: Cmd) -> Self::Future {
32        let mut inner = self.inner.clone();
33
34        Box::pin(async move { inner.req_packed_command(&req).await })
35    }
36}