Skip to content

Commit 2aafa4a

Browse files
authored
Merge pull request #377 from RedisLabsModules/add-new-event-notification-flag
Add NEW key space notification flag
2 parents 8126562 + 023af7f commit 2aafa4a

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

deny.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ include-dependencies = true
281281
name = "redis-module-macros"
282282
allow-globs = ["*"]
283283

284+
[[bans.build.bypass]]
285+
name = "redis-module-macros-internals"
286+
allow-globs = ["*"]
287+
284288
[[bans.build.bypass]]
285289
name = "libloading"
286290
allow-globs = ["*"]

examples/events.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::ptr::NonNull;
55
use std::sync::atomic::{AtomicI64, Ordering};
66

77
static NUM_KEY_MISSES: AtomicI64 = AtomicI64::new(0);
8+
static NUM_KEYS: AtomicI64 = AtomicI64::new(0);
89

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

56+
fn on_new_key(_ctx: &Context, _event_type: NotifyEvent, _event: &str, _key: &[u8]) {
57+
NUM_KEYS.fetch_add(1, Ordering::SeqCst);
58+
}
59+
60+
fn num_keys(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
61+
Ok(RedisValue::Integer(NUM_KEYS.load(Ordering::SeqCst)))
62+
}
5563
//////////////////////////////////////////////////////
5664

5765
redis_module! {
@@ -62,10 +70,12 @@ redis_module! {
6270
commands: [
6371
["events.send", event_send, "", 0, 0, 0],
6472
["events.num_key_miss", num_key_miss, "", 0, 0, 0],
73+
["events.num_keys", num_keys, "", 0, 0, 0],
6574
],
6675
event_handlers: [
6776
[@STRING: on_event],
6877
[@STREAM: on_stream],
6978
[@MISSED: on_key_miss],
79+
[@NEW: on_new_key],
7080
],
7181
}

src/macros.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,19 @@ macro_rules! redis_event_handler {
6868
$crate::raw::Status::Ok as c_int
6969
}
7070

71-
if unsafe {
71+
let all_available_notification_flags = $crate::raw::get_keyspace_notification_flags_all();
72+
let available_wanted_notification_flags = $event_type.intersection(all_available_notification_flags);
73+
if !all_available_notification_flags.contains($event_type) {
74+
let not_supported = $event_type.difference(all_available_notification_flags);
75+
$crate::Context::new($ctx).log_notice(&format!(
76+
"These event notification flags set aren't supported: {not_supported:?}. These flags will be used: {available_wanted_notification_flags:?}"
77+
));
78+
}
79+
80+
if !available_wanted_notification_flags.is_empty() && unsafe {
7281
$crate::raw::RedisModule_SubscribeToKeyspaceEvents.unwrap()(
7382
$ctx,
74-
$event_type.bits(),
83+
available_wanted_notification_flags.bits(),
7584
Some(__handle_event),
7685
)
7786
} == $crate::raw::Status::Err as c_int

src/raw.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,16 @@ bitflags! {
134134
const EXPIRED = REDISMODULE_NOTIFY_EXPIRED;
135135
const EVICTED = REDISMODULE_NOTIFY_EVICTED;
136136
const STREAM = REDISMODULE_NOTIFY_STREAM;
137+
/// Available only starting from Redis `7.0.1`.
138+
const NEW = REDISMODULE_NOTIFY_NEW;
137139
const MODULE = REDISMODULE_NOTIFY_MODULE;
138140
const LOADED = REDISMODULE_NOTIFY_LOADED;
139141
const MISSED = REDISMODULE_NOTIFY_KEY_MISS;
142+
/// Does not include the [`Self::MISSED`] and [`Self::NEW`].
143+
///
144+
/// Includes [`Self::GENERIC`], [`Self::STRING`], [`Self::LIST`],
145+
/// [`Self::SET`], [`Self::HASH`], [`Self::ZSET`], [`Self::EXPIRED`],
146+
/// [`Self::EVICTED`], [`Self::STREAM`], [`Self::MODULE`].
140147
const ALL = REDISMODULE_NOTIFY_ALL;
141148
const TRIMMED = REDISMODULE_NOTIFY_TRIMMED;
142149
}
@@ -905,6 +912,24 @@ pub fn get_keyspace_events() -> NotifyEvent {
905912
}
906913
}
907914

915+
/// Returns all the available notification flags for key-space
916+
/// notifications.
917+
///
918+
/// # Safety
919+
///
920+
/// This function is safe to use as it doesn't perform any work with
921+
/// the [RedisModuleCtx] pointer except for passing it to the redis server.
922+
///
923+
/// # Panics
924+
///
925+
/// Panics when the [RedisModule_GetKeyspaceNotificationFlagsAll] is
926+
/// unavailable.
927+
pub fn get_keyspace_notification_flags_all() -> NotifyEvent {
928+
unsafe {
929+
NotifyEvent::from_bits_truncate(RedisModule_GetKeyspaceNotificationFlagsAll.unwrap()())
930+
}
931+
}
932+
908933
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
909934
pub struct Version {
910935
pub major: i32,

0 commit comments

Comments
 (0)