forked from aembke/fred.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua.rs
36 lines (28 loc) · 1.04 KB
/
lua.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use fred::prelude::*;
use fred::util as fred_utils;
static SCRIPTS: &'static [&'static str] = &[
"return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}",
"return {KEYS[2],KEYS[1],ARGV[1],ARGV[2]}",
"return {KEYS[1],KEYS[2],ARGV[2],ARGV[1]}",
"return {KEYS[2],KEYS[1],ARGV[2],ARGV[1]}",
];
#[tokio::main]
async fn main() -> Result<(), RedisError> {
let config = RedisConfig::default();
let client = RedisClient::new(config);
let _jh = client.connect(None);
let _ = client.wait_for_connect().await?;
for script in SCRIPTS.iter() {
let hash = fred_utils::sha1_hash(script);
if !client.script_exists(&hash).await?.pop().unwrap_or(false) {
let _ = client.script_load(*script).await?;
}
let results = client.evalsha(&hash, vec!["foo", "bar"], vec![1, 2]).await?;
println!("Script results for {}: {:?}", hash, results);
}
// or use eval without script_load
let result = client.eval(SCRIPTS[0], vec!["foo", "bar"], vec![1, 2]).await?;
println!("First script result: {:?}", result);
let _ = client.quit().await;
Ok(())
}