Skip to content

Commit c0c7319

Browse files
authored
add support for notify_keyspace_event (#133)
* add support for notify_keyspace_event * add events example
1 parent 549b814 commit c0c7319

File tree

7 files changed

+50
-6
lines changed

7 files changed

+50
-6
lines changed

examples/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use redis_module::{Context, RedisError, RedisResult, RedisValue, ThreadSafeContext};
4+
use redis_module::{Context, RedisResult, RedisValue, ThreadSafeContext};
55
use std::thread;
66
use std::time::Duration;
77

examples/events.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use redis_module::{Context, NotifyEvent};
4+
use redis_module::{Context, NotifyEvent, RedisError, RedisResult, Status};
55

66
fn on_event(ctx: &Context, event_type: NotifyEvent, event: &str, key: &str) {
77
let msg = format!(
@@ -15,13 +15,27 @@ fn on_stream(ctx: &Context, _event_type: NotifyEvent, _event: &str, _key: &str)
1515
ctx.log_debug("Stream event received!");
1616
}
1717

18+
fn event_send(ctx: &Context, args: Vec<String>) -> RedisResult {
19+
if args.len() > 1 {
20+
return Err(RedisError::WrongArity);
21+
}
22+
23+
let status = ctx.notify_keyspace_event(NotifyEvent::GENERIC, "events.send", "mykey");
24+
match status {
25+
Status::Ok => Ok("Event sent".into()),
26+
Status::Err => Err(RedisError::Str("Generic error")),
27+
}
28+
}
29+
1830
//////////////////////////////////////////////////////
1931

2032
redis_module! {
2133
name: "events",
2234
version: 1,
2335
data_types: [],
24-
commands: [],
36+
commands: [
37+
["events.send", event_send, "", 0, 0, 0],
38+
],
2539
event_handlers: [
2640
[@EXPIRED @EVICTED: on_event],
2741
[@STREAM: on_stream],

examples/timer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use redis_module::{Context, NextArg, RedisError, RedisResult};
4+
use redis_module::{Context, NextArg, RedisResult};
55
use std::time::Duration;
66

77
fn callback(ctx: &Context, data: String) {

src/context/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,14 @@ impl Context {
213213
pub fn get_raw(&self) -> *mut raw::RedisModuleCtx {
214214
return self.ctx;
215215
}
216+
217+
#[cfg(feature = "experimental-api")]
218+
pub fn notify_keyspace_event(
219+
&self,
220+
event_type: raw::NotifyEvent,
221+
event: &str,
222+
keyname: &str,
223+
) -> raw::Status {
224+
raw::notify_keyspace_event(self.ctx, event_type, event, keyname)
225+
}
216226
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ pub use crate::context::thread_safe::{DetachedFromClient, ThreadSafeContext};
2929
#[cfg(feature = "experimental-api")]
3030
pub use crate::raw::NotifyEvent;
3131

32-
pub use crate::raw::Status;
3332
pub use crate::context::Context;
33+
pub use crate::raw::Status;
3434
pub use crate::redismodule::*;
3535

3636
/// Ideally this would be `#[cfg(not(test))]`, but that doesn't work:

src/logging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::LogLevel;
21
use crate::raw;
2+
use crate::LogLevel;
33
use std::ffi::CString;
44
use std::ptr;
55

src/raw.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,23 @@ pub fn subscribe_to_server_event(
474474
) -> Status {
475475
unsafe { RedisModule_SubscribeToServerEvent.unwrap()(ctx, event, callback).into() }
476476
}
477+
478+
#[cfg(feature = "experimental-api")]
479+
pub fn notify_keyspace_event(
480+
ctx: *mut RedisModuleCtx,
481+
event_type: NotifyEvent,
482+
event: &str,
483+
keyname: &str,
484+
) -> Status {
485+
let event = CString::new(event).unwrap();
486+
let keyname = RedisString::create(ctx, keyname);
487+
unsafe {
488+
RedisModule_NotifyKeyspaceEvent.unwrap()(
489+
ctx,
490+
event_type.bits,
491+
event.as_ptr(),
492+
keyname.inner,
493+
)
494+
.into()
495+
}
496+
}

0 commit comments

Comments
 (0)