redis-rs लाइब्रेरी: Rust के लिए Redis क्लाइंट
redis-rs Rust भाषा से Redis के साथ काम करने के लिए मुख्य, परिपक्व और सबसे लोकप्रिय लाइब्रेरी है। यह सिंक्रोनस और एसिंक्रोनस (tokio या async-std पर आधारित) API दोनों प्रदान करती है, और क्लस्टरिंग, पाइपलाइन, Pub/Sub, ट्रांजेक्शन और Lua स्क्रिप्ट को सपोर्ट करती है। यह लाइब्रेरी उच्च प्रदर्शन, टाइप-सेफ्टी और Rust इकोसिस्टम की एर्गोनॉमिक्स के लिए जानी जाती है।
इंस्टॉलेशन और कनेक्शन
Cargo.toml में डिपेंडेंसी जोड़ें:
[dependencies]
redis = { version = "0.25", features = ["tokio-comp"] } # tokio के साथ async के लिए
# या
redis = "0.25" # केवल सिंक्रोनस मोड
Redis से बेसिक कनेक्शन (सिंक्रोनस वर्जन):
use redis::Commands;
fn main() -> redis::RedisResult<()> {
let client = redis::Client::open("redis://127.0.0.1:6379/")?;
let mut con = client.get_connection()?;
// वैल्यू सेट और गेट करना
con.set("key", "value")?;
let val: String = con.get("key")?;
println!("प्राप्त: {}", val);
Ok(())
}
मुख्य सुविधाएँ
1. टाइप किए गए कमांड
लाइब्रेरी स्वचालित रूप से Rust टाइप्स को Redis टाइप्स में और वापस कन्वर्ट करती है। स्ट्रिंग्स, नंबर, टपल, वेक्टर और FromRedisValue / ToRedisArgs ट्रेट के माध्यम से यूज़र-डिफाइंड टाइप्स भी सपोर्टेड हैं।
use redis::Commands;
let mut con = client.get_connection()?;
let count: i32 = con.incr("counter", 2)?; // एटॉमिक इन्क्रीमेंट
let exists: bool = con.exists("nonexistent")?;
let members: Vec<String> = con.smembers("set_key")?;
2. पाइपलाइन
पाइपलाइन आपको एक ही नेटवर्क रिक्वेस्ट में कई कमांड भेजने की अनुमति देती है, जिससे बैच ऑपरेशन काफी तेज़ हो जाते हैं।
use redis::Pipeline;
let mut pipe = Pipeline::new();
pipe.cmd("SET").arg("a").arg(1).ignore();
pipe.cmd("SET").arg("b").arg(2).ignore();
pipe.cmd("GET").arg("a");
let result: i32 = pipe.query(&mut con)?;
println!("a का मान: {}", result);
3. एसिंक्रोनस मोड (async/await)
उच्च-लोड एप्लिकेशन के लिए tokio या async-std पर आधारित एसिंक्रोनस API का उपयोग करें।
use redis::AsyncCommands;
#[tokio::main]
async fn main() -> redis::RedisResult<()> {
let client = redis::Client::open("redis://127.0.0.1:6379/")?;
let mut con = client.get_multiplexed_async_connection().await?;
con.set("async_key", "async_value").await?;
let val: String = con.get("async_key").await?;
println!("Async: {}", val);
Ok(())
}
4. Pub/Sub (प्रकाशक-सब्सक्राइबर)
रीयल-टाइम मैसेजिंग के लिए Pub/Sub पैटर्न का कार्यान्वयन।
use redis::{AsyncCommands, RedisResult};
#[tokio::main]
async fn main() -> RedisResult<()> {
let client = redis::Client::open("redis://127.0.0.1:6379/")?;
// सब्सक्राइबर
let mut pubsub = client.get_async_connection().await?.into_pubsub();
pubsub.subscribe("news").await?;
loop {
let msg = pubsub.next_message().await?;
println!("प्राप्त: {:?}", msg.get_payload::<String>()?);
}
}
5. ट्रांजेक्शन (MULTI/EXEC)
कमांड को एक एटॉमिक ट्रांजेक्शन में समूहित करना।
use redis::Commands;
let mut con = client.get_connection()?;
redis::transaction(&mut con, &["key1", "key2"], |con, pipe| {
pipe.cmd("SET").arg("key1").arg(42).ignore();
pipe.cmd("INCR").arg("key2");
pipe.query(con)
})?;
6. क्लस्टरिंग
Redis Cluster के साथ काम करने के लिए redis::cluster::ClusterClient का उपयोग करें:
use redis::cluster::ClusterClient;
let nodes = vec!["redis://127.0.0.