1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use std::net::ToSocketAddrs;
use std::sync::{Arc, Mutex};
use conhash::{ConsistentHash, Node};
use errors;
use protocol;
#[derive(Debug, Clone)]
struct ClonableProtocol {
connection: Arc<Mutex<protocol::Protocol>>,
}
impl Node for ClonableProtocol {
fn name(&self) -> String {
let protocol = self.clone();
let connection = protocol.connection.lock().unwrap();
connection.connection_info()
}
}
pub struct MemcachedClient {
connections: ConsistentHash<ClonableProtocol>,
}
impl MemcachedClient {
pub fn new<A: ToSocketAddrs>(
addrs: Vec<A>,
connections_per_addr: u8,
) -> Result<MemcachedClient, errors::BMemcachedError> {
let mut ch = ConsistentHash::new();
for addr in &addrs {
for _ in 0..connections_per_addr {
let protocol = protocol::Protocol::connect(addr)?;
ch.add(
&ClonableProtocol { connection: Arc::new(Mutex::new(protocol)) },
1,
);
}
}
Ok(MemcachedClient { connections: ch })
}
pub fn set<K, V>(&self, key: K, value: V, time: u32) -> Result<(), errors::BMemcachedError>
where
K: AsRef<[u8]>,
V: protocol::ToMemcached,
{
let clonable_protocol = self.connections.get(key.as_ref()).unwrap();
let mut protocol = clonable_protocol.connection.lock().unwrap();
protocol.set(key, value, time)
}
pub fn add<K, V>(&self, key: K, value: V, time: u32) -> Result<(), errors::BMemcachedError>
where
K: AsRef<[u8]>,
V: protocol::ToMemcached,
{
let clonable_protocol = self.connections.get(key.as_ref()).unwrap();
let mut protocol = clonable_protocol.connection.lock().unwrap();
protocol.add(key, value, time)
}
pub fn replace<K, V>(&self, key: K, value: V, time: u32) -> Result<(), errors::BMemcachedError>
where
K: AsRef<[u8]>,
V: protocol::ToMemcached,
{
let clonable_protocol = self.connections.get(key.as_ref()).unwrap();
let mut protocol = clonable_protocol.connection.lock().unwrap();
protocol.replace(key, value, time)
}
pub fn get<K, V>(&self, key: K) -> Result<V, errors::BMemcachedError>
where
K: AsRef<[u8]>,
V: protocol::FromMemcached,
{
let clonable_protocol = self.connections.get(key.as_ref()).unwrap();
let mut protocol = clonable_protocol.connection.lock().unwrap();
protocol.get(key)
}
pub fn delete<K>(&self, key: K) -> Result<(), errors::BMemcachedError>
where
K: AsRef<[u8]>,
{
let clonable_protocol = self.connections.get(key.as_ref()).unwrap();
let mut protocol = clonable_protocol.connection.lock().unwrap();
protocol.delete(key)
}
pub fn increment<K>(
&self,
key: K,
amount: u64,
initial: u64,
time: u32,
) -> Result<u64, errors::BMemcachedError>
where
K: AsRef<[u8]>,
{
let clonable_protocol = self.connections.get(key.as_ref()).unwrap();
let mut protocol = clonable_protocol.connection.lock().unwrap();
protocol.increment(key, amount, initial, time)
}
pub fn decrement<K>(
&self,
key: K,
amount: u64,
initial: u64,
time: u32,
) -> Result<u64, errors::BMemcachedError>
where
K: AsRef<[u8]>,
{
let clonable_protocol = self.connections.get(key.as_ref()).unwrap();
let mut protocol = clonable_protocol.connection.lock().unwrap();
protocol.decrement(key, amount, initial, time)
}
}