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

refactor(redis_interface): make the redis command for using scripts to write into redis Generic #6965

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
56 changes: 48 additions & 8 deletions crates/redis_interface/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,20 +854,23 @@ impl super::RedisConnectionPool {
}

#[instrument(level = "DEBUG", skip(self))]
pub async fn incr_keys_using_script<V>(
pub async fn incr_keys_using_script<V, T>(
Aprabhat19 marked this conversation as resolved.
Show resolved Hide resolved
&self,
lua_script: &'static str,
key: Vec<String>,
values: V,
) -> CustomResult<(), errors::RedisError>
) -> CustomResult<T, errors::RedisError>
where
V: TryInto<MultipleValues> + Debug + Send + Sync,
V::Error: Into<fred::error::RedisError> + 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)
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -978,7 +980,45 @@ mod tests {
.collect::<Vec<String>>();

// 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()
})
})
.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::<Vec<_>>();

// Act
let result = pool
.incr_keys_using_script::<_, String>(lua_script, key, 0)
.await;

// Assert Setup
result.is_ok()
Expand Down
Loading