Redis with redis-rs (Rust)
Uses the redis crate 1.8. The code is an ordinary application for the original database; nothing in it knows about MachNoSQL.
The only change
Section titled “The only change”redis://:secret@redis.example.com:6379/0redis://:secret@machnosql.example.com:6379/0The address comes from the environment variable REDIS_URL, so switching to MachNoSQL changes configuration only.
export REDIS_URL='redis://:secret@machnosql.example.com:6379/0'cargo runSource
Section titled “Source”The program as it is kept and tested in the Mach Database NoSQL repository; highlighted lines read the connection setting.
//! Cache, counter, session and leaderboard on Redis, unchanged on MachNoSQL.
use redis::{Commands, RedisResult};
fn main() -> RedisResult<()> { let url = std::env::var("REDIS_URL").expect("set REDIS_URL"); let client = redis::Client::open(url)?; let mut con = client.get_connection()?;
let _: () = con.set_ex("cache:home", "<html>...</html>", 60)?; let cached: String = con.get("cache:home")?; let ttl: i64 = con.ttl("cache:home")?; println!("cached: {cached} ttl: {ttl}");
let views: i64 = con.incr("views:home", 1)?; println!("views: {views}");
let _: () = con.hset_multiple("session:42", &[("user", "ada"), ("cart_items", "3")])?; let session: std::collections::HashMap<String, String> = con.hgetall("session:42")?; println!("session: {session:?}");
let _: () = con.zadd_multiple("leaderboard", &[(120, "ada"), (95, "linus"), (130, "grace")])?; let top: Vec<(String, f64)> = con.zrevrange_withscores("leaderboard", 0, 2)?; println!("top: {top:?}");
let (count,): (i64,) = redis::pipe() .atomic() .incr("orders:count", 1) .lpush("orders:recent", "order-1001") .ignore() .ltrim("orders:recent", 0, 99) .ignore() .query(&mut con)?; println!("pipeline: orders {count}"); Ok(())}[package]name = "machnosql-example-redis"version = "0.1.0"edition = "2024"publish = false
[dependencies]redis = "1.8.1"