Expand description
Lua scripting support for Redis
This module provides functionality for executing Lua scripts on Redis servers using EVAL and EVALSHA commands. Scripts are automatically cached and managed for optimal performance.
§Examples
§Basic Script Execution
use redis_oxide::{Client, ConnectionConfig};
let config = ConnectionConfig::new("redis://localhost:6379");
let client = Client::connect(config).await?;
// Execute a simple Lua script
let script = "return redis.call('GET', KEYS[1])";
let result: Option<String> = client.eval(script, vec!["mykey".to_string()], vec![]).await?;
println!("Result: {:?}", result);§Script with Arguments
use redis_oxide::{Client, ConnectionConfig};
let config = ConnectionConfig::new("redis://localhost:6379");
let client = Client::connect(config).await?;
// Script that increments a counter by a given amount
let script = r#"
local current = redis.call('GET', KEYS[1]) or 0
local increment = tonumber(ARGV[1])
local new_value = tonumber(current) + increment
redis.call('SET', KEYS[1], new_value)
return new_value
"#;
let result: i64 = client.eval(
script,
vec!["counter".to_string()],
vec!["5".to_string()]
).await?;
println!("New counter value: {}", result);§Using Script Manager for Caching
use redis_oxide::{Client, ConnectionConfig, Script};
let config = ConnectionConfig::new("redis://localhost:6379");
let client = Client::connect(config).await?;
// Create a reusable script
let script = Script::new(r#"
local key = KEYS[1]
local value = ARGV[1]
redis.call('SET', key, value)
return redis.call('GET', key)
"#);
// Execute the script (automatically uses EVALSHA if cached)
let result: String = script.execute(
&client,
vec!["mykey".to_string()],
vec!["myvalue".to_string()]
).await?;
println!("Result: {}", result);Modules§
- patterns
- Common Lua script patterns
Structs§
- Script
- A Lua script that can be executed on Redis
- Script
Manager - Script manager for caching and managing multiple scripts