Skip to main content

r2d2_memcache/
connection_manager.rs

1extern crate memcache;
2extern crate r2d2;
3
4use error::Error;
5
6#[derive(Debug)]
7pub struct MemcacheConnectionManager {
8    urls: Vec<String>,
9}
10
11impl MemcacheConnectionManager {
12    /// Creates a new `MemcacheConnectionManager`.
13    ///
14    /// See `memcache::Connection::connect` for a description of the parameter
15    /// types.
16    pub fn new<C: memcache::Connectable>(target: C) -> MemcacheConnectionManager {
17        MemcacheConnectionManager {
18            urls: target.get_urls(),
19        }
20    }
21}
22
23impl r2d2::ManageConnection for MemcacheConnectionManager {
24    type Connection = memcache::Client;
25    type Error = Error;
26
27    fn connect(&self) -> Result<memcache::Client, Error> {
28        memcache::Client::connect(self.urls.clone()).map_err(Error::Other)
29    }
30
31    fn is_valid(&self, connection: &mut memcache::Client) -> Result<(), Error> {
32        match connection.version() {
33            Ok(_) => Ok(()),
34            Err(err) => Err(Error::Other(err)),
35        }
36    }
37
38    fn has_broken(&self, _connection: &mut memcache::Client) -> bool {
39        false
40    }
41}