pub struct BlockingClient { /* private fields */ }
Expand description
Established connection with a Redis server.
Backed by a single TcpStream
, BlockingClient
provides basic network
client functionality (no pooling, retrying, …). Connections are
established using the connect
function.
Requests are issued using the various methods of Client
.
Implementations§
Source§impl BlockingClient
impl BlockingClient
Sourcepub fn get(&mut self, key: &str) -> Result<Option<Bytes>>
pub fn get(&mut self, key: &str) -> Result<Option<Bytes>>
Get the value of key.
If the key does not exist the special value None
is returned.
§Examples
Demonstrates basic usage.
use mini_redis::blocking_client;
fn main() {
let mut client = blocking_client::connect("localhost:6379").unwrap();
let val = client.get("foo").unwrap();
println!("Got = {:?}", val);
}
Sourcepub fn set(&mut self, key: &str, value: Bytes) -> Result<()>
pub fn set(&mut self, key: &str, value: Bytes) -> Result<()>
Set key
to hold the given value
.
The value
is associated with key
until it is overwritten by the next
call to set
or it is removed.
If key already holds a value, it is overwritten. Any previous time to live associated with the key is discarded on successful SET operation.
§Examples
Demonstrates basic usage.
use mini_redis::blocking_client;
fn main() {
let mut client = blocking_client::connect("localhost:6379").unwrap();
client.set("foo", "bar".into()).unwrap();
// Getting the value immediately works
let val = client.get("foo").unwrap().unwrap();
assert_eq!(val, "bar");
}
Sourcepub fn set_expires(
&mut self,
key: &str,
value: Bytes,
expiration: Duration,
) -> Result<()>
pub fn set_expires( &mut self, key: &str, value: Bytes, expiration: Duration, ) -> Result<()>
Set key
to hold the given value
. The value expires after expiration
The value
is associated with key
until one of the following:
- it expires.
- it is overwritten by the next call to
set
. - it is removed.
If key already holds a value, it is overwritten. Any previous time to live associated with the key is discarded on a successful SET operation.
§Examples
Demonstrates basic usage. This example is not guaranteed to always work as it relies on time based logic and assumes the client and server stay relatively synchronized in time. The real world tends to not be so favorable.
use mini_redis::blocking_client;
use std::thread;
use std::time::Duration;
fn main() {
let ttl = Duration::from_millis(500);
let mut client = blocking_client::connect("localhost:6379").unwrap();
client.set_expires("foo", "bar".into(), ttl).unwrap();
// Getting the value immediately works
let val = client.get("foo").unwrap().unwrap();
assert_eq!(val, "bar");
// Wait for the TTL to expire
thread::sleep(ttl);
let val = client.get("foo").unwrap();
assert!(val.is_some());
}
Sourcepub fn publish(&mut self, channel: &str, message: Bytes) -> Result<u64>
pub fn publish(&mut self, channel: &str, message: Bytes) -> Result<u64>
Posts message
to the given channel
.
Returns the number of subscribers currently listening on the channel. There is no guarantee that these subscribers receive the message as they may disconnect at any time.
§Examples
Demonstrates basic usage.
use mini_redis::blocking_client;
fn main() {
let mut client = blocking_client::connect("localhost:6379").unwrap();
let val = client.publish("foo", "bar".into()).unwrap();
println!("Got = {:?}", val);
}
Sourcepub fn subscribe(self, channels: Vec<String>) -> Result<BlockingSubscriber>
pub fn subscribe(self, channels: Vec<String>) -> Result<BlockingSubscriber>
Subscribes the client to the specified channels.
Once a client issues a subscribe command, it may no longer issue any
non-pub/sub commands. The function consumes self
and returns a
BlockingSubscriber
.
The BlockingSubscriber
value is used to receive messages as well as
manage the list of channels the client is subscribed to.