pub struct Client { /* private fields */ }
Expand description
Client for operating connection pool
Implementations§
Source§impl Client
impl Client
Sourcepub fn connect<T: Connectable>(urls: T) -> Result<Self>
pub fn connect<T: Connectable>(urls: T) -> Result<Self>
Create a memcached client instance and connect to memcached server. The default connection pool has only one connection.
§Example
let client = memcached::Client::connect("memcache://127.0.0.1:12345")?;
Sourcepub fn connect_with<T: Connectable>(
urls: T,
pool_size: u64,
hash_function: fn(&str) -> u64,
) -> Result<Self>
pub fn connect_with<T: Connectable>( urls: T, pool_size: u64, hash_function: fn(&str) -> u64, ) -> Result<Self>
Create a client, you can specify multiple url, connection pool size, key hash connection pool function.
§Example
let client = memcached::Client::connect_with(vec!["memcache://127.0.0.1:12345".to_owned()], 2, |s|1)?;
Sourcepub async fn version(&self) -> Result<HashMap<String, String>>
pub async fn version(&self) -> Result<HashMap<String, String>>
Get server version
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
let version = client.version().await?;
Sourcepub async fn get<V: DeserializeOwned + 'static, K: AsRef<str>>(
&self,
key: K,
) -> Result<Option<V>>
pub async fn get<V: DeserializeOwned + 'static, K: AsRef<str>>( &self, key: K, ) -> Result<Option<V>>
Get a value by key
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
let t: Option<String> = client.get("get_none").await?;
assert_eq!(t, None);
Sourcepub async fn set<V: Serialize + 'static, K: AsRef<str>>(
&self,
key: K,
value: V,
expiration: u32,
) -> Result<()>
pub async fn set<V: Serialize + 'static, K: AsRef<str>>( &self, key: K, value: V, expiration: u32, ) -> Result<()>
Set a key with associate value into memcached server with expiration seconds.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.set("abc", "hello", 100).await?;
let t: Option<String> = client.get("abc").await?;
assert_eq!(t, Some("hello".to_owned()));
Sourcepub async fn flush(&self) -> Result<()>
pub async fn flush(&self) -> Result<()>
Flush all cache on memcached server immediately.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.set("flush_test", "hello", 100).await?;
client.flush().await?;
let t: Option<String> = client.get("flush_test").await?;
assert_eq!(t, None);
Sourcepub async fn flush_with_delay(&self, delay: u32) -> Result<()>
pub async fn flush_with_delay(&self, delay: u32) -> Result<()>
Flush all cache on memcached server with a delay seconds.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.set("flush_with_delay_test", "hello", 100).await?;
client.flush_with_delay(2).await?;
let t: Option<String> = client.get("flush_with_delay_test").await?;
assert_eq!(t, Some("hello".to_owned()));
async_std::task::sleep(core::time::Duration::from_secs(2)).await;
let t: Option<String> = client.get("flush_with_delay_test").await?;
assert_eq!(t, None);
Sourcepub async fn add<V: Serialize + 'static, K: AsRef<str>>(
&self,
key: K,
value: V,
expiration: u32,
) -> Result<()>
pub async fn add<V: Serialize + 'static, K: AsRef<str>>( &self, key: K, value: V, expiration: u32, ) -> Result<()>
Add a key with associate value into memcached server with expiration seconds.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.delete("add_test").await?;
client.add("add_test", "hello", 100).await?;
// repeat add KeyExists
client.add("add_test", "hello233", 100).await.unwrap_err();
let t: Option<String> = client.get("add_test").await?;
assert_eq!(t, Some("hello".to_owned()));
Sourcepub async fn replace<V: Serialize + 'static, K: AsRef<str>>(
&self,
key: K,
value: V,
expiration: u32,
) -> Result<()>
pub async fn replace<V: Serialize + 'static, K: AsRef<str>>( &self, key: K, value: V, expiration: u32, ) -> Result<()>
Replace a key with associate value into memcached server with expiration seconds.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.delete("replace_test").await?;
// KeyNotFound
client.replace("replace_test", "hello", 100).await.unwrap_err();
client.add("replace_test", "hello", 100).await?;
client.replace("replace_test", "hello233", 100).await?;
let t: Option<String> = client.get("replace_test").await?;
assert_eq!(t, Some("hello233".to_owned()));
Sourcepub async fn append<V: Serialize + 'static, K: AsRef<str>>(
&self,
key: K,
value: V,
) -> Result<()>
pub async fn append<V: Serialize + 'static, K: AsRef<str>>( &self, key: K, value: V, ) -> Result<()>
Append value to the key.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.set("append_test", "hello", 100).await?;
client.append("append_test", ", 233").await?;
let t: Option<String> = client.get("append_test").await?;
assert_eq!(t, Some("hello, 233".to_owned()));
Sourcepub async fn prepend<V: Serialize + 'static, K: AsRef<str>>(
&self,
key: K,
value: V,
) -> Result<()>
pub async fn prepend<V: Serialize + 'static, K: AsRef<str>>( &self, key: K, value: V, ) -> Result<()>
Prepend value to the key.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.set("prepend_test", "hello", 100).await?;
client.prepend("prepend_test", "233! ").await?;
let t: Option<String> = client.get("prepend_test").await?;
assert_eq!(t, Some("233! hello".to_owned()));
Sourcepub async fn delete<K: AsRef<str>>(&self, key: K) -> Result<bool>
pub async fn delete<K: AsRef<str>>(&self, key: K) -> Result<bool>
Delete a key from memcached server.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.add("delete_test", "hello", 100).await?;
let t: Option<String> = client.get("delete_test").await?;
assert_eq!(t, Some("hello".to_owned()));
client.delete("delete_test").await?;
let t: Option<String> = client.get("delete_test").await?;
assert_eq!(t, None);
Sourcepub async fn increment<K: AsRef<str>>(&self, key: K, amount: u64) -> Result<u64>
pub async fn increment<K: AsRef<str>>(&self, key: K, amount: u64) -> Result<u64>
Increment the value with amount.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.set("increment_test", 100, 100).await?;
client.increment("increment_test", 10).await?;
assert_eq!(120, client.increment("increment_test", 10).await.unwrap());
let t: Option<u64> = client.get("increment_test").await?;
assert_eq!(t, Some(120));
Sourcepub async fn decrement<K: AsRef<str>>(&self, key: K, amount: u64) -> Result<u64>
pub async fn decrement<K: AsRef<str>>(&self, key: K, amount: u64) -> Result<u64>
Decrement the value with amount.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.set("decrement_test", 100, 100).await?;
let t = client.decrement("decrement_test", 10).await?;
assert_eq!(80, client.decrement("decrement_test", 10).await.unwrap());
let t: Option<u64> = client.get("decrement_test").await?;
assert_eq!(t.unwrap(), 80);
Sourcepub async fn touch<K: AsRef<str>>(
&self,
key: K,
expiration: u32,
) -> Result<bool>
pub async fn touch<K: AsRef<str>>( &self, key: K, expiration: u32, ) -> Result<bool>
Set a new expiration time for a exist key.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.set("touch_test", "100", 100).await?;
async_std::task::sleep(core::time::Duration::from_secs(1)).await;
let t: Option<String> = client.get("touch_test").await?;
assert_eq!(t, Some("100".to_owned()));
client.touch("touch_test", 1).await?;
async_std::task::sleep(core::time::Duration::from_secs(1)).await;
let t: Option<String> = client.get("touch_test").await?;
assert_eq!(t, None);
Sourcepub async fn stats(&self) -> Result<Vec<(String, HashMap<String, String>)>>
pub async fn stats(&self) -> Result<Vec<(String, HashMap<String, String>)>>
Get all servers’ statistics.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
let t = client.stats().await?;
Sourcepub async fn gets<V: DeserializeOwned + 'static, K: AsRef<str>>(
&self,
keys: &[K],
) -> Result<HashMap<String, (V, u32, Option<u64>)>>
pub async fn gets<V: DeserializeOwned + 'static, K: AsRef<str>>( &self, keys: &[K], ) -> Result<HashMap<String, (V, u32, Option<u64>)>>
Get multiple keys from memcached server. Using this function instead of calling get
multiple times can reduce netwark workloads.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.set("gets_test1", "100", 100).await?;
client.set("gets_test2", "200", 100).await?;
let t = client
.gets::<String, _>(&["gets_test1", "gets_test2"])
.await?;
Sourcepub async fn cas<V: Serialize + 'static, K: AsRef<str>>(
&self,
key: K,
value: V,
expiration: u32,
cas_id: u64,
) -> Result<bool>
pub async fn cas<V: Serialize + 'static, K: AsRef<str>>( &self, key: K, value: V, expiration: u32, cas_id: u64, ) -> Result<bool>
Compare and swap a key with the associate value into memcached server with expiration seconds.
cas_id
should be obtained from a previous gets
call.
§Example
let client = memcached::connect("memcache://127.0.0.1:12345")?;
client.set("cas_test1", "100", 100).await?;
let t = client
.gets::<String, _>(&["cas_test1"])
.await
?;
let k = t.get("cas_test1").unwrap();
assert_eq!(&k.0, "100");
let t = client
.cas("cas_test1", "200", 100, k.2.unwrap() - 1)
.await
?;
let t = client.get::<String, _>("cas_test1").await?;
assert_eq!(t.unwrap(), "100".to_owned());
let t = client
.cas("cas_test1", "300", 100, k.2.unwrap())
.await
?;
let t = client.get::<String, _>("cas_test1").await?;
assert_eq!(t.unwrap(), "300".to_owned());;