Struct Context

Source
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

Source

pub fn create_timer<F, T: 'static>( &self, period: Duration, callback: F, data: T, ) -> RedisModuleTimerID
where 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.

Source

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.

Source

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

Source§

impl Context

Source

pub fn server_info(&self, section: &str) -> ServerInfo

Source§

impl Context

Source

pub const fn new(ctx: *mut RedisModuleCtx) -> Self

Source

pub const fn dummy() -> Self

Source

pub fn log(&self, level: RedisLogLevel, message: &str)

Source

pub fn log_debug(&self, message: &str)

Source

pub fn log_notice(&self, message: &str)

Source

pub fn log_verbose(&self, message: &str)

Source

pub fn log_warning(&self, message: &str)

Source

pub fn auto_memory(&self)

§Panics

Will panic if RedisModule_AutoMemory is missing in redismodule.h

Source

pub fn is_keys_position_request(&self) -> bool

§Panics

Will panic if RedisModule_IsKeysPositionRequest is missing in redismodule.h

Source

pub fn key_at_pos(&self, pos: i32)

§Panics

Will panic if RedisModule_KeyAtPos is missing in redismodule.h

Source

pub fn call<'a, T: Into<StrCallArgs<'a>>>( &self, command: &str, args: T, ) -> RedisResult

Source

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.

Source

pub fn reply_simple_string(&self, s: &str) -> Status

Source

pub fn reply_error_string(&self, s: &str) -> Status

Source

pub fn reply_with_key(&self, result: RedisValueKey) -> Status

Source

pub fn reply(&self, result: RedisResult) -> Status

§Panics

Will panic if methods used are missing in redismodule.h

Source

pub fn open_key(&self, key: &RedisString) -> RedisKey

Source

pub fn open_key_with_flags( &self, key: &RedisString, flags: KeyFlags, ) -> RedisKey

Source

pub fn open_key_writable(&self, key: &RedisString) -> RedisKeyWritable

Source

pub fn open_key_writable_with_flags( &self, key: &RedisString, flags: KeyFlags, ) -> RedisKeyWritable

Source

pub fn replicate_verbatim(&self)

Source

pub fn replicate<'a, T: Into<StrCallArgs<'a>>>(&self, command: &str, args: T)

Replicate command to the replica and AOF.

Source

pub fn create_string<T: Into<Vec<u8>>>(&self, s: T) -> RedisString

Source

pub const fn get_raw(&self) -> *mut RedisModuleCtx

Source

pub unsafe fn export_shared_api(&self, func: *const c_void, name: *const c_char)

Source

pub fn notify_keyspace_event( &self, event_type: NotifyEvent, event: &str, keyname: &RedisString, ) -> Status

Source

pub fn current_command_name(&self) -> Result<String, RedisError>

Source

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

Source

pub fn get_redis_version_rm_call(&self) -> Result<Version, RedisError>

Returns the redis version by calling “info server” API and parsing the reply

Source

pub fn version_from_info(info: RedisValue) -> Result<Version, RedisError>

Source

pub fn set_module_options(&self, options: ModuleOptions)

Source

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
Source

pub fn get_current_user(&self) -> RedisString

Return the current user name attached to the context

Source

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.

Source

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.

Source

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:

  1. It is safe to perform any write operation.
  2. 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.

Source

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.

Trait Implementations§

Source§

impl Borrow<Context> for ContextGuard

Source§

fn borrow(&self) -> &Context

Immutably borrows from an owned value. Read more
Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl RedisLockIndicator for Context

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.