Skip to main content

prepare_command

Function prepare_command 

Source
pub fn prepare_command<'a, E, R: DeserializeOwned>(
    executor: E,
    command: impl Into<Command>,
) -> PreparedCommand<'a, E, R>
Expand description

Shortcut function to creating a PreparedCommand.

Use it to add a command rustis does not implement, keeping the fluent client.mycommand("key").await shape. Every built-in command trait is written this way.

use rustis::{
    client::{Client, PreparedCommand, prepare_command},
    resp::cmd,
};
use serde::Serialize;

trait MyCommands<'a> {
    #[must_use]
    fn myget(self, key: impl Serialize) -> PreparedCommand<'a, Self, String>
    where
        Self: Sized,
    {
        prepare_command(self, cmd("MYGET").key(key))
    }
}

// Implement it per executor. Add `&mut Pipeline` and `&mut Transaction` to
// make the command queueable into a batch.
impl<'a> MyCommands<'a> for &'a Client {}

Add Redis keys with CommandBuilder::key, everything else with arg. Cluster routing reads the keys, so a key passed as an argument is sent to the wrong node.