Skip to main content

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 async_memcached::{Client, Error};
11mod config;
12
13/// Type alias for using [`deadpool::managed::RecycleResult`] with [`redis`].
14type RecycleResult = deadpool::managed::RecycleResult<Error>;
15
16pub use self::config::{Config, ConfigError};
17pub use deadpool::managed::reexports::*;
18deadpool::managed_reexports!(
19    "memcached",
20    Manager,
21    deadpool::managed::Object<Manager>,
22    Error,
23    ConfigError
24);
25
26/// The manager for creating and recyling memcache connections
27pub struct Manager {
28    addr: String,
29}
30
31impl Manager {
32    /// Create a new manager for the given address.
33    pub fn new(addr: impl Into<String>) -> Self {
34        Self { addr: addr.into() }
35    }
36}
37
38impl deadpool::managed::Manager for Manager {
39    type Type = Client;
40    type Error = Error;
41
42    async fn create(&self) -> Result<Client, Error> {
43        Client::new(&self.addr).await
44    }
45
46    async fn recycle(&self, conn: &mut Client, _: &Metrics) -> RecycleResult {
47        match conn.version().await {
48            Ok(_) => Ok(()),
49            Err(e) => Err(e.into()),
50        }
51    }
52}