Struct Client

Source
pub struct Client { /* private fields */ }
Expand description

Client for operating connection pool

Implementations§

Source§

impl Client

Source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,