The first argument is the context, and the second is always a null terminated
C string with the command name. The third argument is the format specifier
where each character corresponds to the type of the arguments that will follow.
In the above case `"sc"` means a RedisModuleString object, and a null
terminated C string. The other arguments are just the two arguments as
specified. In fact `argv[1]` is a RedisModuleString and `"10"` is a null
terminated C string.
This is the full list of format specifiers:
***c** -- Null terminated C string pointer.
***b** -- C buffer, two arguments needed: C string pointer and `size_t` length.
***s** -- RedisModuleString as received in `argv` or by other Redis module APIs returning a RedisModuleString object.
***l** -- Long long integer.
***v** -- Array of RedisModuleString objects.
***!** -- This modifier just tells the function to replicate the command to slaves and AOF. It is ignored from the point of view of arguments parsing.
The function returns a `RedisModuleCallReply` object on success, on
error NULL is returned.
NULL is returned when the command name is invalid, the format specifier uses
characters that are not recognized, or when the command is called with the
wrong number of arguments. In the above cases the `errno` var is set to `EINVAL`. NULL is also returned when, in an instance with Cluster enabled, the target
keys are about non local hash slots. In this case `errno` is set to `EPERM`.
## Working with RedisModuleCallReply objects.
`RedisModuleCall` returns reply objects that can be accessed using the
`RedisModule_CallReply*` family of functions.
In order to obtain the type or reply (corresponding to one of the data types
supported by the Redis protocol), the function `RedisModule_CallReplyType()`
Note that you often want to proceed with a command both if the key
Valid IDs are from 1 to 2^64-1. If 0 is returned it means there is no way
is of the expected type, or if it's empty.
to fetch the ID in the context the function was currently called.
## Low level access to keys
## `RM_GetSelectedDb`
Low level access to keys allow to perform operations on value objects associated
int RM_GetSelectedDb(RedisModuleCtx *ctx);
to keys directly, with a speed similar to what Redis uses internally to
implement the built-in commands.
Once a key is opened, a key pointer is returned that will be used with all the
Return the currently selected DB.
other low level API calls in order to perform operations on the key or its
associated value.
Because the API is meant to be very fast, it cannot do too many run-time
## `RM_SelectDb`
checks, so the user must be aware of certain rules to follow:
* Opening the same key multiple times where at least one instance is opened for writing, is undefined and may lead to crashes.
int RM_SelectDb(RedisModuleCtx *ctx, int newid);
* While a key is open, it should only be accessed via the low level key API. For example opening a key, then calling DEL on the same key using the `RedisModule_Call()` API will result into a crash. However it is safe to open a key, perform some operation with the low level API, closing it, then using other APIs to manage the same key, and later opening it again to do some more work.
In order to open a key the `RedisModule_OpenKey` function is used. It returns
Change the currently selected DB. Returns an error if the id
a key pointer, that we'll use with all the next calls to access and modify
is out of range.
the value:
RedisModuleKey *key;
Note that the client will retain the currently selected DB even after
The first argument is the context, and the second is always a null terminated
C string with the command name. The third argument is the format specifier
where each character corresponds to the type of the arguments that will follow.
In the above case `"sc"` means a RedisModuleString object, and a null
terminated C string. The other arguments are just the two arguments as
specified. In fact `argv[1]` is a RedisModuleString and `"10"` is a null
terminated C string.
This is the full list of format specifiers:
***c** -- Null terminated C string pointer.
***b** -- C buffer, two arguments needed: C string pointer and `size_t` length.
***s** -- RedisModuleString as received in `argv` or by other Redis module APIs returning a RedisModuleString object.
***l** -- Long long integer.
***v** -- Array of RedisModuleString objects.
***!** -- This modifier just tells the function to replicate the command to slaves and AOF. It is ignored from the point of view of arguments parsing.
The function returns a `RedisModuleCallReply` object on success, on
error NULL is returned.
NULL is returned when the command name is invalid, the format specifier uses
characters that are not recognized, or when the command is called with the
wrong number of arguments. In the above cases the `errno` var is set to `EINVAL`. NULL is also returned when, in an instance with Cluster enabled, the target
keys are about non local hash slots. In this case `errno` is set to `EPERM`.
## Working with RedisModuleCallReply objects.
`RedisModuleCall` returns reply objects that can be accessed using the
`RedisModule_CallReply*` family of functions.
In order to obtain the type or reply (corresponding to one of the data types
supported by the Redis protocol), the function `RedisModule_CallReplyType()`
Note that you often want to proceed with a command both if the key
is of the expected type, or if it's empty.
## Low level access to keys
Low level access to keys allow to perform operations on value objects associated
to keys directly, with a speed similar to what Redis uses internally to
implement the built-in commands.
Once a key is opened, a key pointer is returned that will be used with all the
other low level API calls in order to perform operations on the key or its
associated value.
Because the API is meant to be very fast, it cannot do too many run-time
checks, so the user must be aware of certain rules to follow:
* Opening the same key multiple times where at least one instance is opened for writing, is undefined and may lead to crashes.
* While a key is open, it should only be accessed via the low level key API. For example opening a key, then calling DEL on the same key using the `RedisModule_Call()` API will result into a crash. However it is safe to open a key, perform some operation with the low level API, closing it, then using other APIs to manage the same key, and later opening it again to do some more work.
In order to open a key the `RedisModule_OpenKey` function is used. It returns
a key pointer, that we'll use with all the next calls to access and modify