use rustis::{
Result,
client::Client,
commands::{ScriptingCommands, StringCommands},
};
const COMPARE_AND_SET: &str = r"
if redis.call('GET', KEYS[1]) == ARGV[1] then
redis.call('SET', KEYS[1], ARGV[2])
return 1
end
return 0
";
#[tokio::main]
async fn main() -> Result<()> {
let client = Client::connect("127.0.0.1:6379").await?;
client.set("script_key", "old").await?;
let changed: i64 = client
.eval(COMPARE_AND_SET, ["script_key"], ["old", "new"])
.await?;
println!("changed: {changed}");
let sha1: String = client.script_load(COMPARE_AND_SET).await?;
let changed: i64 = client
.evalsha(&sha1, ["script_key"], ["new", "newer"])
.await?;
println!("changed: {changed}");
let library = r"#!lua name=examples
redis.register_function('bump', function(keys, args)
return redis.call('INCRBY', keys[1], args[1])
end)
";
let _: String = client.function_load(true, library).await?;
let total: i64 = client.fcall("bump", ["counter"], [7]).await?;
println!("counter: {total}");
Ok(())
}