deadpool_memcached/
lib.rs

1//! # Deadpool for Memcache
2//!
3//! Deadpool is a dead simple async pool for connections and objects of any type.
4//!
5//! This crate implements a [`deadpool`](https://crates.io/crates/deadpool) manager for
6//! [`async-memcached`](https://crates.io/crates/async-memcached).  We specifically force users to
7//! connect via TCP as there is no existing mechanism to parameterize how to deal with different
8//! unerlying connection types at the moment.
9#![deny(warnings, missing_docs)]
10use std::convert::Infallible;
11
12use async_memcached::{Client, Error};
13
14/// Type alias for using [`deadpool::managed::RecycleResult`] with [`redis`].
15type RecycleResult = deadpool::managed::RecycleResult<Error>;
16
17type ConfigError = Infallible;
18
19pub use deadpool::managed::reexports::*;
20deadpool::managed_reexports!(
21    "memcached",
22    Manager,
23    deadpool::managed::Object<Manager>,
24    Error,
25    ConfigError
26);
27
28/// The manager for creating and recyling memcache connections
29pub struct Manager {
30    addr: String,
31}
32
33impl Manager {
34    /// Create a new manager for the given address.
35    pub fn new(addr: impl Into<String>) -> Self {
36        Self { addr: addr.into() }
37    }
38}
39
40impl deadpool::managed::Manager for Manager {
41    type Type = Client;
42    type Error = Error;
43
44    async fn create(&self) -> Result<Client, Error> {
45        Client::new(&self.addr).await
46    }
47
48    async fn recycle(&self, conn: &mut Client, _: &Metrics) -> RecycleResult {
49        match conn.version().await {
50            Ok(_) => Ok(()),
51            Err(e) => Err(e.into()),
52        }
53    }
54}