Struct memcache_async::ascii::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,
impl<S> Protocol<S>where
S: AsyncRead + AsyncWrite + Unpin,
sourcepub fn new(io: S) -> Self
pub fn new(io: S) -> Self
Creates the ASCII protocol on a stream.
Examples found in repository?
examples/tcp-simple.rs (line 24)
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
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("{} <addr>", args[0]);
return;
}
let addr = &args[1];
block_on(async move {
let (key, val) = ("foo", "bar");
let stream = TcpStream::connect(addr).expect("Failed to create stream");
// "futures::io::AllowStdIo" is used here to make the stream
// work with AsyncIO. This shouldn't be used in production
// since it will block current thread. Use something like
// romio or tokio instead.
let mut cache = Protocol::new(AllowStdIo::new(stream));
cache
.set(&key, val.as_bytes(), 0)
.await
.expect("Failed to set key");
let v = cache.get(&key).await.expect("Failed to get key");
assert_eq!(v, val.as_bytes());
});
}
sourcepub async fn get<K: AsRef<[u8]>>(&mut self, key: K) -> Result<Vec<u8>, Error>
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 30)
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
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("{} <addr>", args[0]);
return;
}
let addr = &args[1];
block_on(async move {
let (key, val) = ("foo", "bar");
let stream = TcpStream::connect(addr).expect("Failed to create stream");
// "futures::io::AllowStdIo" is used here to make the stream
// work with AsyncIO. This shouldn't be used in production
// since it will block current thread. Use something like
// romio or tokio instead.
let mut cache = Protocol::new(AllowStdIo::new(stream));
cache
.set(&key, val.as_bytes(), 0)
.await
.expect("Failed to set key");
let v = cache.get(&key).await.expect("Failed to get key");
assert_eq!(v, val.as_bytes());
});
}
sourcepub async fn get_multi<K: AsRef<[u8]>>(
&mut self,
keys: &[K]
) -> Result<HashMap<String, Vec<u8>>, Error>
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.
sourcepub async fn get_prefix<K: Display>(
&mut self,
key_prefix: K,
limit: Option<usize>
) -> Result<HashMap<String, Vec<u8>>, Error>
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.
sourcepub async fn add<K: Display>(
&mut self,
key: K,
val: &[u8],
expiration: u32
) -> Result<(), Error>
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.
sourcepub async fn set<K: Display>(
&mut self,
key: K,
val: &[u8],
expiration: u32
) -> Result<(), Error>
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 26)
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
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("{} <addr>", args[0]);
return;
}
let addr = &args[1];
block_on(async move {
let (key, val) = ("foo", "bar");
let stream = TcpStream::connect(addr).expect("Failed to create stream");
// "futures::io::AllowStdIo" is used here to make the stream
// work with AsyncIO. This shouldn't be used in production
// since it will block current thread. Use something like
// romio or tokio instead.
let mut cache = Protocol::new(AllowStdIo::new(stream));
cache
.set(&key, val.as_bytes(), 0)
.await
.expect("Failed to set key");
let v = cache.get(&key).await.expect("Failed to get key");
assert_eq!(v, val.as_bytes());
});
}
sourcepub async fn delete<K: Display>(&mut self, key: K) -> Result<(), Error>
pub async fn delete<K: Display>(&mut self, key: K) -> Result<(), Error>
Delete a key and don’t wait for response.
Auto Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more