Struct Protocol

Source
pub struct Protocol<S> { /* private fields */ }
Expand description

Memcache ASCII protocol implementation.

Implementations§

Source§

impl<S> Protocol<S>
where S: AsyncRead + AsyncWrite + Unpin,

Source

pub fn new(io: S) -> Self

Creates the ASCII protocol on a stream.

Examples found in repository?
examples/tcp-simple.rs (line 25)
8fn main() {
9    let args: Vec<String> = env::args().collect();
10    if args.len() < 2 {
11        eprintln!("{} <addr>", args[0]);
12        return;
13    }
14
15    let addr = &args[1];
16
17    block_on(async move {
18        let (key, val) = ("foo", "bar");
19        let stream = TcpStream::connect(addr).expect("Failed to create stream");
20
21        // "futures::io::AllowStdIo" is used here to make the stream
22        // work with AsyncIO. This shouldn't be used in production
23        // since it will block current thread. Use something like
24        // romio or tokio instead.
25        let mut cache = Protocol::new(AllowStdIo::new(stream));
26        cache
27            .set(&key, val.as_bytes(), 0)
28            .await
29            .expect("Failed to set key");
30
31        let v = cache.get(&key).await.expect("Failed to get key");
32        assert_eq!(v, val.as_bytes());
33    });
34}
Source

pub async fn get<K: AsRef<[u8]>>(&mut self, key: K) -> Result<Vec<u8>, Error>

Returns the value for given key as bytes. If the value doesn’t exist, ErrorKind::NotFound is returned.

Examples found in repository?
examples/tcp-simple.rs (line 31)
8fn main() {
9    let args: Vec<String> = env::args().collect();
10    if args.len() < 2 {
11        eprintln!("{} <addr>", args[0]);
12        return;
13    }
14
15    let addr = &args[1];
16
17    block_on(async move {
18        let (key, val) = ("foo", "bar");
19        let stream = TcpStream::connect(addr).expect("Failed to create stream");
20
21        // "futures::io::AllowStdIo" is used here to make the stream
22        // work with AsyncIO. This shouldn't be used in production
23        // since it will block current thread. Use something like
24        // romio or tokio instead.
25        let mut cache = Protocol::new(AllowStdIo::new(stream));
26        cache
27            .set(&key, val.as_bytes(), 0)
28            .await
29            .expect("Failed to set key");
30
31        let v = cache.get(&key).await.expect("Failed to get key");
32        assert_eq!(v, val.as_bytes());
33    });
34}
Source

pub async fn get_multi<K: AsRef<[u8]>>( &mut self, keys: &[K], ) -> Result<HashMap<String, Vec<u8>>, Error>

Returns values for multiple keys in a single call as a HashMap from keys to found values. If a key is not present in memcached it will be absent from returned map.

Source

pub async fn get_prefix<K: Display>( &mut self, key_prefix: K, limit: Option<usize>, ) -> Result<HashMap<String, Vec<u8>>, Error>

Get up to limit keys which match the given prefix. Returns a HashMap from keys to found values. This is not part of the Memcached standard, but some servers implement it nonetheless.

Source

pub async fn add<K: Display>( &mut self, key: K, val: &[u8], expiration: u32, ) -> Result<(), Error>

Add a key. If the value exists, ErrorKind::AlreadyExists is returned.

Source

pub async fn set<K: Display>( &mut self, key: K, val: &[u8], expiration: u32, ) -> Result<(), Error>

Set key to given value and don’t wait for response.

Examples found in repository?
examples/tcp-simple.rs (line 27)
8fn main() {
9    let args: Vec<String> = env::args().collect();
10    if args.len() < 2 {
11        eprintln!("{} <addr>", args[0]);
12        return;
13    }
14
15    let addr = &args[1];
16
17    block_on(async move {
18        let (key, val) = ("foo", "bar");
19        let stream = TcpStream::connect(addr).expect("Failed to create stream");
20
21        // "futures::io::AllowStdIo" is used here to make the stream
22        // work with AsyncIO. This shouldn't be used in production
23        // since it will block current thread. Use something like
24        // romio or tokio instead.
25        let mut cache = Protocol::new(AllowStdIo::new(stream));
26        cache
27            .set(&key, val.as_bytes(), 0)
28            .await
29            .expect("Failed to set key");
30
31        let v = cache.get(&key).await.expect("Failed to get key");
32        assert_eq!(v, val.as_bytes());
33    });
34}
Source

pub async fn append<K: Display>( &mut self, key: K, val: &[u8], ) -> Result<(), Error>

Append bytes to the value in memcached and don’t wait for response.

Source

pub async fn delete<K: Display>(&mut self, key: K) -> Result<(), Error>

Delete a key and don’t wait for response.

Source

pub async fn version(&mut self) -> Result<String, Error>

Return the version of the remote server.

Source

pub async fn flush(&mut self) -> Result<(), Error>

Delete all keys from the cache.

Source

pub async fn increment<K: AsRef<[u8]>>( &mut self, key: K, amount: u64, ) -> Result<u64, Error>

Increment a specific integer stored with a key by a given value. If the value doesn’t exist, ErrorKind::NotFound is returned. Otherwise the new value is returned

Source

pub async fn decrement<K: AsRef<[u8]>>( &mut self, key: K, amount: u64, ) -> Result<u64, Error>

Decrement a specific integer stored with a key by a given value. If the value doesn’t exist, ErrorKind::NotFound is returned. Otherwise the new value is returned

Source

pub async fn gets_cas<K: AsRef<[u8]>>( &mut self, key: K, ) -> Result<(Vec<u8>, u64), Error>

Call gets to also return CAS id, which can be used to run a second CAS command

Source

pub async fn cas<K: Display>( &mut self, key: K, val: &[u8], cas_id: u64, expiration: u32, ) -> Result<bool, Error>

Source

pub async fn append_or_vivify<K: Display>( &mut self, key: K, val: &[u8], ttl: u32, ) -> Result<(), Error>

Append bytes to the value in memcached, and creates the key if it is missing instead of failing compared to simple append

Auto Trait Implementations§

§

impl<S> Freeze for Protocol<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for Protocol<S>
where S: RefUnwindSafe,

§

impl<S> Send for Protocol<S>
where S: Send,

§

impl<S> Sync for Protocol<S>
where S: Sync,

§

impl<S> Unpin for Protocol<S>
where S: Unpin,

§

impl<S> UnwindSafe for Protocol<S>
where S: UnwindSafe,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.