[][src]Attribute Macro iredismodule_macros::rcmd

#[rcmd]

A wrapper of module command func.

It's can have five attrs.

#[rcmd("hello.leftpad", "", 0, 0, 0)]
#[rcmd("hello.leftpad")]
#[rcmd("helloacl.authglobal", "no-auth")]
#[rcmd("hello.hcopy", "write deny-oom", 1, 1, 1)]

The first attr command name, it is required.

The second attr is 'strflags' specify the behavior of the command and should be passed as a C string composed of space separated words, like for example "write deny-oom". The set of flags are:

  • "write": The command may modify the data set (it may also read from it).
  • "readonly": The command returns data from keys but never writes.
  • "admin": The command is an administrative command (may change replication or perform similar tasks).
  • "deny-oom": The command may use additional memory and should be denied during out of memory conditions.
  • "deny-script": Don't allow this command in Lua scripts.
  • "allow-loading": Allow this command while the server is loading data. Only commands not interacting with the data set should be allowed to run in this mode. If not sure don't use this flag.
  • "pubsub": The command publishes things on Pub/Sub channels.
  • "random": The command may have different outputs even starting from the same input arguments and key values.
  • "allow-stale": The command is allowed to run on slaves that don't serve stale data. Don't use if you don't know what this means.
  • "no-monitor": Don't propagate the command on monitor. Use this if the command has sensible data among the arguments.
  • "no-slowlog": Don't log this command in the slowlog. Use this if the command has sensible data among the arguments.
  • "fast": The command time complexity is not greater than O(log(N)) where N is the size of the collection or anything else representing the normal scalability issue with the command.
  • "getkeys-api": The command implements the interface to return the arguments that are keys. Used when start/stop/step is not enough because of the command syntax.
  • "no-cluster": The command should not register in Redis Cluster since is not designed to work with it because, for example, is unable to report the position of the keys, programmatically creates key names, or any other reason.
  • "no-auth": This command can be run by an un-authenticated client. Normally this is used by a command that is used to authenticate a client.

The las three attrs means first_key, last_key and key_step.

#[rcmd("hello.simple", "readonly", 0, 0, 0)]
fn hello_simple(ctx: &mut Context, _args: Vec<RStr>) -> RResult {
    let db = ctx.get_select_db();
    Ok(db.into())
}

This macro expands above code:

fn hello_simple(ctx: &mut Context, _args: Vec<RStr>) -> RResult {
    let db = ctx.get_select_db();
    Ok(db.into())
}
extern "C" fn hello_simple_c(
    ctx: *mut iredismodule::raw::RedisModuleCtx,
    argv: *mut *mut iredismodule::raw::RedisModuleString,
    argc: std::os::raw::c_int,
) -> std::os::raw::c_int {
    use iredismodule::FromPtr;
    let mut context = iredismodule::context::Context::from_ptr(ctx);
    let response = hello_simple(&mut context, iredismodule::parse_args(argv, argc));
    context.reply(response);
    iredismodule::raw::REDISMODULE_OK as std::os::raw::c_int
}
fn hello_simple_cmd(
    ctx: &mut iredismodule::context::Context,
) -> Result<(), iredismodule::error::Error> {
    ctx.create_cmd(
        "hello.simple",
        hello_simple_c,
        "readonly",
        0usize,
        0usize,
        0usize,
    )
}

The hello_simple fn is the origin fn.

The hello_simple_c fn is a c wrapper function which will be apply to ffi or callback.

The hello_simple_cmd fn is entrypoint to register command.

The ***_cmd fn will be applyed to define_macro

define_module! {
    name: "simple",
    version: 1,
    data_types: [],
    init_funcs: [],
    commands: [
        hello_simple_cmd, // <- used here
    ]
}