Skip to content

Commit

Permalink
Merge pull request #377 from RedisLabsModules/add-new-event-notificat…
Browse files Browse the repository at this point in the history
…ion-flag

Add NEW key space notification flag
  • Loading branch information
iddm authored Dec 5, 2023
2 parents 8126562 + 023af7f commit 2aafa4a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ include-dependencies = true
name = "redis-module-macros"
allow-globs = ["*"]

[[bans.build.bypass]]
name = "redis-module-macros-internals"
allow-globs = ["*"]

[[bans.build.bypass]]
name = "libloading"
allow-globs = ["*"]
Expand Down
10 changes: 10 additions & 0 deletions examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::ptr::NonNull;
use std::sync::atomic::{AtomicI64, Ordering};

static NUM_KEY_MISSES: AtomicI64 = AtomicI64::new(0);
static NUM_KEYS: AtomicI64 = AtomicI64::new(0);

fn on_event(ctx: &Context, event_type: NotifyEvent, event: &str, key: &[u8]) {
if key == b"num_sets" {
Expand Down Expand Up @@ -52,6 +53,13 @@ fn num_key_miss(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(RedisValue::Integer(NUM_KEY_MISSES.load(Ordering::SeqCst)))
}

fn on_new_key(_ctx: &Context, _event_type: NotifyEvent, _event: &str, _key: &[u8]) {
NUM_KEYS.fetch_add(1, Ordering::SeqCst);
}

fn num_keys(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(RedisValue::Integer(NUM_KEYS.load(Ordering::SeqCst)))
}
//////////////////////////////////////////////////////

redis_module! {
Expand All @@ -62,10 +70,12 @@ redis_module! {
commands: [
["events.send", event_send, "", 0, 0, 0],
["events.num_key_miss", num_key_miss, "", 0, 0, 0],
["events.num_keys", num_keys, "", 0, 0, 0],
],
event_handlers: [
[@STRING: on_event],
[@STREAM: on_stream],
[@MISSED: on_key_miss],
[@NEW: on_new_key],
],
}
13 changes: 11 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,19 @@ macro_rules! redis_event_handler {
$crate::raw::Status::Ok as c_int
}

if unsafe {
let all_available_notification_flags = $crate::raw::get_keyspace_notification_flags_all();
let available_wanted_notification_flags = $event_type.intersection(all_available_notification_flags);
if !all_available_notification_flags.contains($event_type) {
let not_supported = $event_type.difference(all_available_notification_flags);
$crate::Context::new($ctx).log_notice(&format!(
"These event notification flags set aren't supported: {not_supported:?}. These flags will be used: {available_wanted_notification_flags:?}"
));
}

if !available_wanted_notification_flags.is_empty() && unsafe {
$crate::raw::RedisModule_SubscribeToKeyspaceEvents.unwrap()(
$ctx,
$event_type.bits(),
available_wanted_notification_flags.bits(),
Some(__handle_event),
)
} == $crate::raw::Status::Err as c_int
Expand Down
25 changes: 25 additions & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,16 @@ bitflags! {
const EXPIRED = REDISMODULE_NOTIFY_EXPIRED;
const EVICTED = REDISMODULE_NOTIFY_EVICTED;
const STREAM = REDISMODULE_NOTIFY_STREAM;
/// Available only starting from Redis `7.0.1`.
const NEW = REDISMODULE_NOTIFY_NEW;
const MODULE = REDISMODULE_NOTIFY_MODULE;
const LOADED = REDISMODULE_NOTIFY_LOADED;
const MISSED = REDISMODULE_NOTIFY_KEY_MISS;
/// Does not include the [`Self::MISSED`] and [`Self::NEW`].
///
/// Includes [`Self::GENERIC`], [`Self::STRING`], [`Self::LIST`],
/// [`Self::SET`], [`Self::HASH`], [`Self::ZSET`], [`Self::EXPIRED`],
/// [`Self::EVICTED`], [`Self::STREAM`], [`Self::MODULE`].
const ALL = REDISMODULE_NOTIFY_ALL;
const TRIMMED = REDISMODULE_NOTIFY_TRIMMED;
}
Expand Down Expand Up @@ -905,6 +912,24 @@ pub fn get_keyspace_events() -> NotifyEvent {
}
}

/// Returns all the available notification flags for key-space
/// notifications.
///
/// # Safety
///
/// This function is safe to use as it doesn't perform any work with
/// the [RedisModuleCtx] pointer except for passing it to the redis server.
///
/// # Panics
///
/// Panics when the [RedisModule_GetKeyspaceNotificationFlagsAll] is
/// unavailable.
pub fn get_keyspace_notification_flags_all() -> NotifyEvent {
unsafe {
NotifyEvent::from_bits_truncate(RedisModule_GetKeyspaceNotificationFlagsAll.unwrap()())
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version {
pub major: i32,
Expand Down

0 comments on commit 2aafa4a

Please sign in to comment.