pub struct Context {
pub ctx: *mut RedisModuleCtx,
}
Expand description
Context
is a structure that’s designed to give us a high-level interface to
the Redis module API by abstracting away the raw C FFI calls.
Fields§
§ctx: *mut RedisModuleCtx
Implementations§
Source§impl Context
impl Context
Sourcepub fn create_timer<F, T: 'static>(
&self,
period: Duration,
callback: F,
data: T,
) -> RedisModuleTimerIDwhere
F: FnOnce(&Self, T),
pub fn create_timer<F, T: 'static>(
&self,
period: Duration,
callback: F,
data: T,
) -> RedisModuleTimerIDwhere
F: FnOnce(&Self, T),
Wrapper for RedisModule_CreateTimer
.
This function takes ownership of the provided data, and transfers it to Redis. The callback will get the original data back in a type safe manner. When the callback is done, the data will be dropped.
Sourcepub fn stop_timer<T>(
&self,
timer_id: RedisModuleTimerID,
) -> Result<T, RedisError>
pub fn stop_timer<T>( &self, timer_id: RedisModuleTimerID, ) -> Result<T, RedisError>
Wrapper for RedisModule_StopTimer
.
The caller is responsible for specifying the correct type for the returned data.
This function has no way to know what the original type of the data was, so the
same data type that was used for create_timer
needs to be passed here to ensure
their types are identical.
Sourcepub fn get_timer_info<T>(
&self,
timer_id: RedisModuleTimerID,
) -> Result<(Duration, &T), RedisError>
pub fn get_timer_info<T>( &self, timer_id: RedisModuleTimerID, ) -> Result<(Duration, &T), RedisError>
Wrapper for RedisModule_GetTimerInfo
.
The caller is responsible for specifying the correct type for the returned data.
This function has no way to know what the original type of the data was, so the
same data type that was used for create_timer
needs to be passed here to ensure
their types are identical.
Source§impl Context
impl Context
pub fn block_client(&self) -> BlockedClient
Source§impl Context
impl Context
pub const fn new(ctx: *mut RedisModuleCtx) -> Self
pub const fn dummy() -> Self
pub fn log(&self, level: RedisLogLevel, message: &str)
pub fn log_debug(&self, message: &str)
pub fn log_notice(&self, message: &str)
pub fn log_verbose(&self, message: &str)
pub fn log_warning(&self, message: &str)
Sourcepub fn auto_memory(&self)
pub fn auto_memory(&self)
§Panics
Will panic if RedisModule_AutoMemory
is missing in redismodule.h
Sourcepub fn is_keys_position_request(&self) -> bool
pub fn is_keys_position_request(&self) -> bool
§Panics
Will panic if RedisModule_IsKeysPositionRequest
is missing in redismodule.h
Sourcepub fn key_at_pos(&self, pos: i32)
pub fn key_at_pos(&self, pos: i32)
§Panics
Will panic if RedisModule_KeyAtPos
is missing in redismodule.h
pub fn call<'a, T: Into<StrCallArgs<'a>>>( &self, command: &str, args: T, ) -> RedisResult
Sourcepub fn call_ext<'a, T: Into<StrCallArgs<'a>>, R: From<CallResult<'static>>>(
&self,
command: &str,
options: &CallOptions,
args: T,
) -> R
pub fn call_ext<'a, T: Into<StrCallArgs<'a>>, R: From<CallResult<'static>>>( &self, command: &str, options: &CallOptions, args: T, ) -> R
Invoke a command on Redis and return the result Unlike ‘call’ this API also allow to pass a CallOption to control different aspects of the command invocation.
pub fn str_as_legal_resp_string(s: &str) -> CString
pub fn reply_simple_string(&self, s: &str) -> Status
pub fn reply_error_string(&self, s: &str) -> Status
pub fn reply_with_key(&self, result: RedisValueKey) -> Status
Sourcepub fn reply(&self, result: RedisResult) -> Status
pub fn reply(&self, result: RedisResult) -> Status
§Panics
Will panic if methods used are missing in redismodule.h
pub fn open_key(&self, key: &RedisString) -> RedisKey
pub fn open_key_with_flags( &self, key: &RedisString, flags: KeyFlags, ) -> RedisKey
pub fn open_key_writable(&self, key: &RedisString) -> RedisKeyWritable
pub fn open_key_writable_with_flags( &self, key: &RedisString, flags: KeyFlags, ) -> RedisKeyWritable
pub fn replicate_verbatim(&self)
Sourcepub fn replicate<'a, T: Into<StrCallArgs<'a>>>(&self, command: &str, args: T)
pub fn replicate<'a, T: Into<StrCallArgs<'a>>>(&self, command: &str, args: T)
Replicate command to the replica and AOF.
pub fn create_string<T: Into<Vec<u8>>>(&self, s: T) -> RedisString
pub const fn get_raw(&self) -> *mut RedisModuleCtx
§Safety
Sourcepub fn notify_keyspace_event(
&self,
event_type: NotifyEvent,
event: &str,
keyname: &RedisString,
) -> Status
pub fn notify_keyspace_event( &self, event_type: NotifyEvent, event: &str, keyname: &RedisString, ) -> Status
§Safety
pub fn current_command_name(&self) -> Result<String, RedisError>
Sourcepub fn get_redis_version(&self) -> Result<Version, RedisError>
pub fn get_redis_version(&self) -> Result<Version, RedisError>
Returns the redis version either by calling RedisModule_GetServerVersion
API,
Or if it is not available, by calling “info server” API and parsing the reply
Sourcepub fn get_redis_version_rm_call(&self) -> Result<Version, RedisError>
pub fn get_redis_version_rm_call(&self) -> Result<Version, RedisError>
Returns the redis version by calling “info server” API and parsing the reply
pub fn version_from_info(info: RedisValue) -> Result<Version, RedisError>
pub fn set_module_options(&self, options: ModuleOptions)
Sourcepub fn get_flags(&self) -> ContextFlags
pub fn get_flags(&self) -> ContextFlags
Return ContextFlags object that allows to check properties related to the state of the current Redis instance such as:
- Role (master/slave)
- Loading RDB/AOF
- Execution mode such as multi exec or Lua
Sourcepub fn get_current_user(&self) -> RedisString
pub fn get_current_user(&self) -> RedisString
Return the current user name attached to the context
Sourcepub fn authenticate_user(
&self,
user_name: &RedisString,
) -> Result<ContextUserScope<'_>, RedisError>
pub fn authenticate_user( &self, user_name: &RedisString, ) -> Result<ContextUserScope<'_>, RedisError>
Attach the given user to the current context so each operation performed from now on using this context will be validated againts this new user. Return [ContextUserScope] which make sure to unset the user when freed and can not outlive the current Context.
Sourcepub fn acl_check_key_permission(
&self,
user_name: &RedisString,
key_name: &RedisString,
permissions: &AclPermissions,
) -> Result<(), RedisError>
pub fn acl_check_key_permission( &self, user_name: &RedisString, key_name: &RedisString, permissions: &AclPermissions, ) -> Result<(), RedisError>
Verify the the given user has the give ACL permission on the given key. Return Ok(()) if the user has the permissions or error (with relevant error message) if the validation failed.
Sourcepub fn add_post_notification_job<F: FnOnce(&Context) + 'static>(
&self,
callback: F,
) -> APIResult<Status>
pub fn add_post_notification_job<F: FnOnce(&Context) + 'static>( &self, callback: F, ) -> APIResult<Status>
When running inside a key space notification callback, it is dangerous and highly discouraged to perform any write operation. In order to still perform write actions in this scenario, Redis provides this API ([add_post_notification_job]) that allows to register a job callback which Redis will call when the following condition holds:
- It is safe to perform any write operation.
- The job will be called atomically along side the key space notification.
Notice, one job might trigger key space notifications that will trigger more jobs. This raises a concerns of entering an infinite loops, we consider infinite loops as a logical bug that need to be fixed in the module, an attempt to protect against infinite loops by halting the execution could result in violation of the feature correctness and so Redis will make no attempt to protect the module from infinite loops.
Sourcepub fn avoid_replication_traffic(&self) -> bool
pub fn avoid_replication_traffic(&self) -> bool
Returns true if a client sent the CLIENT PAUSE command to the server or if Redis Cluster does a manual failover, pausing the clients. This is needed when we have a master with replicas, and want to write, without adding further data to the replication channel, that the replicas replication offset, match the one of the master. When this happens, it is safe to failover the master without data loss.
However modules may generate traffic by calling commands or directly send data to the replication stream.
So modules may want to try to avoid very heavy background work that has the effect of creating data to the replication channel, when this function returns true. This is mostly useful for modules that have background garbage collection tasks, or that do writes and replicate such writes periodically in timer callbacks or other periodic callbacks.