| Crates.io | sal-redisclient |
| lib.rs | sal-redisclient |
| version | 0.1.0 |
| created_at | 2025-07-01 05:59:38.203436+00 |
| updated_at | 2025-07-01 05:59:38.203436+00 |
| description | SAL Redis Client - Redis client wrapper with connection management and Rhai integration |
| homepage | |
| repository | https://git.threefold.info/herocode/sal |
| max_upload_size | |
| id | 1732809 |
| size | 81,846 |
sal-redisclient)A robust Redis client wrapper for Rust applications that provides connection management, automatic reconnection, and a simple interface for executing Redis commands.
Add this to your Cargo.toml:
[dependencies]
sal-redisclient = "0.1.0"
$HOME/hero/var/myredis.sock)REDISDB environment variable to select the Redis database (defaults to 0)use crate::redisclient::execute;
use redis::cmd;
// Execute a simple SET command
let mut set_cmd = redis::cmd("SET");
set_cmd.arg("my_key").arg("my_value");
let result: redis::RedisResult<()> = execute(&mut set_cmd);
// Execute a GET command
let mut get_cmd = redis::cmd("GET");
get_cmd.arg("my_key");
let value: redis::RedisResult<String> = execute(&mut get_cmd);
if let Ok(val) = value {
println!("Value: {}", val);
}
use crate::redisclient::{get_redis_client, reset};
// Get the Redis client directly
let client = get_redis_client()?;
// Execute a command using the client
let mut cmd = redis::cmd("HSET");
cmd.arg("my_hash").arg("field1").arg("value1");
let result: redis::RedisResult<()> = client.execute(&mut cmd);
// Reset the Redis client connection
reset()?;
The module provides a builder pattern for flexible configuration:
use crate::redisclient::{RedisConfigBuilder, with_config};
// Create a configuration builder
let config = RedisConfigBuilder::new()
.host("redis.example.com")
.port(6379)
.db(1)
.username("user")
.password("secret")
.use_tls(true)
.connection_timeout(30);
// Connect with the configuration
let client = with_config(config)?;
You can explicitly configure a Unix socket connection:
use crate::redisclient::{RedisConfigBuilder, with_config};
// Create a configuration builder for Unix socket
let config = RedisConfigBuilder::new()
.use_unix_socket(true)
.socket_path("/path/to/redis.sock")
.db(1);
// Connect with the configuration
let client = with_config(config)?;
REDISDB: Specifies the Redis database number to use (default: 0)REDIS_HOST: Specifies the Redis host (default: 127.0.0.1)REDIS_PORT: Specifies the Redis port (default: 6379)REDIS_USERNAME: Specifies the Redis username for authenticationREDIS_PASSWORD: Specifies the Redis password for authenticationHOME: Used to determine the path to the Redis Unix socket$HOME/hero/var/myredis.sockredis://127.0.0.1/The module provides detailed error messages that include:
The module includes both unit tests and integration tests:
Run the tests with:
cargo test --lib redisclient::tests
The Redis client is wrapped in an Arc<Mutex<>> to ensure thread safety when accessing the global instance.