[][src]Struct memcached::Client

pub struct Client { /* fields omitted */ }

Client for operating connection pool

Implementations

impl Client[src]

pub fn connect<T: Connectable>(urls: T) -> Result<Self>[src]

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")?;

pub fn connect_with<T: Connectable>(
    urls: T,
    pool_size: u64,
    hash_function: fn(_: &str) -> u64
) -> Result<Self>
[src]

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)?;

pub async fn version<'_>(&'_ self) -> Result<HashMap<String, String>>[src]

Get server version

Example

let client = memcached::connect("memcache://127.0.0.1:12345")?;
let version = client.version().await?;

pub async fn get<'_, V: DeserializeOwned + 'static, K: AsRef<str>>(
    &'_ self,
    key: K
) -> Result<Option<V>>
[src]

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);

pub async fn set<'_, V: Serialize + 'static, K: AsRef<str>>(
    &'_ self,
    key: K,
    value: V,
    expiration: u32
) -> Result<()>
[src]

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()));

pub async fn flush<'_>(&'_ self) -> Result<()>[src]

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);

pub async fn flush_with_delay<'_>(&'_ self, delay: u32) -> Result<()>[src]

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);

pub async fn add<'_, V: Serialize + 'static, K: AsRef<str>>(
    &'_ self,
    key: K,
    value: V,
    expiration: u32
) -> Result<()>
[src]

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()));

pub async fn replace<'_, V: Serialize + 'static, K: AsRef<str>>(
    &'_ self,
    key: K,
    value: V,
    expiration: u32
) -> Result<()>
[src]

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()));

pub async fn append<'_, V: Serialize + 'static, K: AsRef<str>>(
    &'_ self,
    key: K,
    value: V
) -> Result<()>
[src]

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()));

pub async fn prepend<'_, V: Serialize + 'static, K: AsRef<str>>(
    &'_ self,
    key: K,
    value: V
) -> Result<()>
[src]

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()));

pub async fn delete<'_, K: AsRef<str>>(&'_ self, key: K) -> Result<bool>[src]

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);

pub async fn increment<'_, K: AsRef<str>>(
    &'_ self,
    key: K,
    amount: u64
) -> Result<u64>
[src]

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));

pub async fn decrement<'_, K: AsRef<str>>(
    &'_ self,
    key: K,
    amount: u64
) -> Result<u64>
[src]

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);

pub async fn touch<'_, K: AsRef<str>>(
    &'_ self,
    key: K,
    expiration: u32
) -> Result<bool>
[src]

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);

pub async fn stats<'_>(
    &'_ self
) -> Result<Vec<(String, HashMap<String, String>)>>
[src]

Get all servers' statistics.

Example

let client = memcached::connect("memcache://127.0.0.1:12345")?;
let t = client.stats().await?;

pub async fn gets<'_, '_, V: DeserializeOwned + 'static, K: AsRef<str>>(
    &'_ self,
    keys: &'_ [K]
) -> Result<HashMap<String, (V, u32, Option<u64>)>>
[src]

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?;

pub async fn cas<'_, V: Serialize + 'static, K: AsRef<str>>(
    &'_ self,
    key: K,
    value: V,
    expiration: u32,
    cas_id: u64
) -> Result<bool>
[src]

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());;

Trait Implementations

impl Clone for Client[src]

Auto Trait Implementations

impl !RefUnwindSafe for Client

impl Send for Client

impl Sync for Client

impl Unpin for Client

impl !UnwindSafe for Client

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.