Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added keymiss event support #289

Merged
merged 4 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion examples/events.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#[macro_use]
extern crate redis_module;

use redis_module::{Context, NotifyEvent, RedisError, RedisResult, RedisString, Status};
use redis_module::{
Context, NotifyEvent, RedisError, RedisResult, RedisString, RedisValue, Status,
};
use std::sync::atomic::{AtomicI64, Ordering};

static NUM_KEY_MISSES: AtomicI64 = AtomicI64::new(0);
iddm marked this conversation as resolved.
Show resolved Hide resolved

fn on_event(ctx: &Context, event_type: NotifyEvent, event: &str, key: &str) {
let msg = format!(
Expand All @@ -28,6 +33,14 @@ fn event_send(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
}
}

fn on_key_miss(_ctx: &Context, _event_type: NotifyEvent, _event: &str, _key: &str) {
NUM_KEY_MISSES.fetch_add(1, Ordering::SeqCst);
}

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

//////////////////////////////////////////////////////

redis_module! {
Expand All @@ -36,10 +49,12 @@ redis_module! {
data_types: [],
commands: [
["events.send", event_send, "", 0, 0, 0],
["events.num_key_miss", num_key_miss, "", 0, 0, 0],
],
event_handlers: [
[@EXPIRED @EVICTED: on_event],
[@STREAM: on_stream],
[@MISSED: on_key_miss],
]
}

Expand Down
1 change: 1 addition & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ bitflags! {
const STREAM = REDISMODULE_NOTIFY_STREAM;
const MODULE = REDISMODULE_NOTIFY_MODULE;
const LOADED = REDISMODULE_NOTIFY_LOADED;
const MISSED = REDISMODULE_NOTIFY_KEY_MISS;
const ALL = REDISMODULE_NOTIFY_ALL;
}
}
Expand Down
22 changes: 20 additions & 2 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::utils::{get_redis_connection, start_redis_server_with_module};
use anyhow::Context;
use anyhow::Result;
use redis::RedisError;
use redis::RedisResult;
use redis::{RedisError, RedisResult};

mod utils;

Expand Down Expand Up @@ -323,3 +322,22 @@ fn test_verify_acl_on_user() -> Result<()> {

Ok(())
}

#[test]
fn test_key_miss_event() -> Result<()> {
let port: u16 = 6492;
let _guards = vec![start_redis_server_with_module("events", port)
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;

let res: usize = redis::cmd("events.num_key_miss").query(&mut con)?;
assert_eq!(res, 0);

let _: RedisResult<String> = redis::cmd("GET").arg(&["x"]).query(&mut con);

let res: usize = redis::cmd("events.num_key_miss").query(&mut con)?;
assert_eq!(res, 1);

Ok(())
}