From 01dfaf59511ce188f569e57cf1200e6024b747cf Mon Sep 17 00:00:00 2001 From: Aprabhat19 Date: Thu, 2 Jan 2025 13:19:24 +0530 Subject: [PATCH 1/4] make the redis command to for using scripts to write into redis Generic --- crates/redis_interface/src/commands.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/crates/redis_interface/src/commands.rs b/crates/redis_interface/src/commands.rs index 19497d6fbb83..65103522aa51 100644 --- a/crates/redis_interface/src/commands.rs +++ b/crates/redis_interface/src/commands.rs @@ -854,20 +854,23 @@ impl super::RedisConnectionPool { } #[instrument(level = "DEBUG", skip(self))] - pub async fn incr_keys_using_script( + pub async fn incr_keys_using_script( &self, lua_script: &'static str, key: Vec, values: V, - ) -> CustomResult<(), errors::RedisError> + ) -> CustomResult where V: TryInto + Debug + Send + Sync, V::Error: Into + Send + Sync, + T: serde::de::DeserializeOwned + FromRedis, { - self.pool + let val: T = self + .pool .eval(lua_script, key, values) .await - .change_context(errors::RedisError::IncrementHashFieldFailed) + .change_context(errors::RedisError::IncrementHashFieldFailed)?; + Ok(val) } } @@ -960,11 +963,10 @@ mod tests { .await .expect("failed to create redis connection pool"); let lua_script = r#" - local results = {} for i = 1, #KEYS do - results[i] = redis.call("INCRBY", KEYS[i], ARGV[i]) + redis.call("INCRBY", KEYS[i], ARGV[i]) end - return results + return "#; let mut keys_and_values = HashMap::new(); for i in 0..10 { @@ -978,7 +980,9 @@ mod tests { .collect::>(); // Act - let result = pool.incr_keys_using_script(lua_script, key, values).await; + let result = pool + .incr_keys_using_script::<_, ()>(lua_script, key, values) + .await; // Assert Setup result.is_ok() From f81ab32f6f6bedd9d42924b7472dbde8f8d260b6 Mon Sep 17 00:00:00 2001 From: Aprabhat19 Date: Fri, 3 Jan 2025 14:15:06 +0530 Subject: [PATCH 2/4] add a test case to test the script --- crates/redis_interface/src/commands.rs | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/crates/redis_interface/src/commands.rs b/crates/redis_interface/src/commands.rs index 65103522aa51..9fa1f6b1e817 100644 --- a/crates/redis_interface/src/commands.rs +++ b/crates/redis_interface/src/commands.rs @@ -991,6 +991,42 @@ mod tests { .await .expect("Spawn block failure"); + assert!(is_success); + } + #[tokio::test] + async fn test_getting_keys_using_scripts() { + let is_success = tokio::task::spawn_blocking(move || { + futures::executor::block_on(async { + // Arrange + let pool = RedisConnectionPool::new(&RedisSettings::default()) + .await + .expect("failed to create redis connection pool"); + let lua_script = r#" + local results = {} + for i = 1, #KEYS do + results[i] = redis.call("GET", KEYS[i]) + end + return results + "#; + let mut keys_and_values = HashMap::new(); + for i in 0..10 { + keys_and_values.insert(format!("key{}", i), i); + } + + let key = keys_and_values.keys().cloned().collect::>(); + + // Act + let result = pool + .incr_keys_using_script::<_, String>(lua_script, key, 0) + .await; + + // Assert Setup + result.is_ok() + }) + }) + .await + .expect("Spawn block failure"); + assert!(is_success); } } From 6ec985e450977b6385f078c8b71b7fe49eaadc6c Mon Sep 17 00:00:00 2001 From: Aprabhat19 Date: Mon, 6 Jan 2025 14:32:07 +0530 Subject: [PATCH 3/4] naming changes --- crates/redis_interface/src/commands.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/redis_interface/src/commands.rs b/crates/redis_interface/src/commands.rs index 2676a8dbd269..908ca0e8a4c4 100644 --- a/crates/redis_interface/src/commands.rs +++ b/crates/redis_interface/src/commands.rs @@ -849,7 +849,7 @@ impl super::RedisConnectionPool { } #[instrument(level = "DEBUG", skip(self))] - pub async fn incr_keys_using_script( + pub async fn implement_redis_functionalities_using_scripts( &self, lua_script: &'static str, key: Vec, @@ -976,7 +976,7 @@ mod tests { // Act let result = pool - .incr_keys_using_script::<_, ()>(lua_script, key, values) + .implement_redis_functionalities_using_scripts::<_, ()>(lua_script, key, values) .await; // Assert Setup @@ -1012,7 +1012,7 @@ mod tests { // Act let result = pool - .incr_keys_using_script::<_, String>(lua_script, key, 0) + .implement_redis_functionalities_using_scripts::<_, String>(lua_script, key, 0) .await; // Assert Setup From b2080a91e3578712c5d65f5a9e83d23bd276d615 Mon Sep 17 00:00:00 2001 From: Aprabhat19 Date: Mon, 6 Jan 2025 16:43:33 +0530 Subject: [PATCH 4/4] naming changes --- crates/redis_interface/src/commands.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/redis_interface/src/commands.rs b/crates/redis_interface/src/commands.rs index 908ca0e8a4c4..56e9ab981043 100644 --- a/crates/redis_interface/src/commands.rs +++ b/crates/redis_interface/src/commands.rs @@ -849,7 +849,7 @@ impl super::RedisConnectionPool { } #[instrument(level = "DEBUG", skip(self))] - pub async fn implement_redis_functionalities_using_scripts( + pub async fn evaluate_redis_script( &self, lua_script: &'static str, key: Vec, @@ -976,7 +976,7 @@ mod tests { // Act let result = pool - .implement_redis_functionalities_using_scripts::<_, ()>(lua_script, key, values) + .evaluate_redis_script::<_, ()>(lua_script, key, values) .await; // Assert Setup @@ -1012,7 +1012,7 @@ mod tests { // Act let result = pool - .implement_redis_functionalities_using_scripts::<_, String>(lua_script, key, 0) + .evaluate_redis_script::<_, String>(lua_script, key, 0) .await; // Assert Setup