Module embedded_redis::commands::set
source · Expand description
Abstraction of SET command.
For general information about this command, see the Redis documentation.
§Basic usage
let mut stack = Stack::default();
let clock = StandardClock::default();
let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
let client = connection_handler.connect(&mut stack, Some(&clock)).unwrap();
let command = SetCommand::new("key", "value");
let _ = client.send(command);
§Expiration (EX, PX, EXAT, PXAT)
Setting TTL can be achieved in the following way. Fore more details s. ExpirationPolicy enum.
// Expires in 120 seconds
let command = SetCommand::new("key", "value")
.expires(ExpirationPolicy::Seconds(120));
§Exclusive condition (NX/XX)
Manage set condition. Fore more details s. Exclusivity enum.
Using this options affects the return type. s. ExclusiveSetResponse
// Just set the key if its not existing yet
let command = SetCommand::new("key", "value")
.set_exclusive(Exclusivity::SetIfMissing);
§Return previous value (!GET)
Returns the previous value stored at the given key.
Using this options affects the return type. s. ReturnPreviousResponse
// Just set the key if its not existing yet
let command = SetCommand::new("key", "value")
.return_previous();
§Shorthand
Client provides a shorthand method for this command.
// Using &str arguments
let _ = client.set("key", "value");
// Using String arguments
let _ = client.set("key".to_string(), "value".to_string());
// Using Bytes arguments
let _ = client.set(Bytes::from_static(b"key"), Bytes::from_static(b"value"));
Structs§
Enums§
Type Aliases§
- Regular response if neither !GET or NX/XX option is set. Indicates that SET operation was successful
- Response if NX/XX option was set.
- Response if !GET option is used.