redis-oxide 0.2.3

High-performance async Redis client for Rust with automatic cluster support, multiplexing, and advanced features
Documentation
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! 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
//!
//! ```no_run
//! use redis_oxide::{Client, ConnectionConfig};
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! 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);
//! # Ok(())
//! # }
//! ```
//!
//! ## Script with Arguments
//!
//! ```no_run
//! use redis_oxide::{Client, ConnectionConfig};
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! 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);
//! # Ok(())
//! # }
//! ```
//!
//! ## Using Script Manager for Caching
//!
//! ```no_run
//! use redis_oxide::{Client, ConnectionConfig, Script};
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! 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);
//! # Ok(())
//! # }
//! ```

use crate::core::{
    error::{RedisError, RedisResult},
    value::RespValue,
};
use sha1::{Digest, Sha1};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::sync::Arc;
use tokio::sync::RwLock;

/// A Lua script that can be executed on Redis
#[derive(Debug, Clone)]
pub struct Script {
    /// The Lua script source code
    source: String,
    /// SHA1 hash of the script (for EVALSHA)
    sha: String,
}

impl Script {
    /// Create a new script from Lua source code
    ///
    /// The script is automatically hashed for use with EVALSHA.
    ///
    /// # Examples
    ///
    /// ```
    /// use redis_oxide::Script;
    ///
    /// let script = Script::new("return redis.call('GET', KEYS[1])");
    /// println!("Script SHA: {}", script.sha());
    /// ```
    pub fn new(source: impl Into<String>) -> Self {
        let source = source.into();
        let sha = calculate_sha1(&source);

        Self { source, sha }
    }

    /// Get the SHA1 hash of the script
    #[must_use]
    pub fn sha(&self) -> &str {
        &self.sha
    }

    /// Get the source code of the script
    #[must_use]
    pub fn source(&self) -> &str {
        &self.source
    }

    /// Execute the script on the given client
    ///
    /// This method will first try to use EVALSHA (if the script is cached on the server),
    /// and fall back to EVAL if the script is not cached.
    ///
    /// # Arguments
    ///
    /// * `client` - The Redis client to execute the script on
    /// * `keys` - List of Redis keys that the script will access (KEYS array in Lua)
    /// * `args` - List of arguments to pass to the script (ARGV array in Lua)
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig, Script};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let script = Script::new("return KEYS[1] .. ':' .. ARGV[1]");
    ///
    /// let result: String = script.execute(
    ///     &client,
    ///     vec!["user".to_string()],
    ///     vec!["123".to_string()]
    /// ).await?;
    ///
    /// assert_eq!(result, "user:123");
    /// # Ok(())
    /// # }
    /// ```
    pub async fn execute<T>(
        &self,
        client: &crate::Client,
        keys: Vec<String>,
        args: Vec<String>,
    ) -> RedisResult<T>
    where
        T: TryFrom<RespValue>,
        T::Error: Into<RedisError>,
    {
        // First try EVALSHA
        match client.evalsha(&self.sha, keys.clone(), args.clone()).await {
            Ok(result) => Ok(result),
            Err(RedisError::Protocol(msg)) if msg.contains("NOSCRIPT") => {
                // Script not cached, use EVAL
                client.eval(&self.source, keys, args).await
            }
            Err(e) => Err(e),
        }
    }

    /// Load the script into Redis cache
    ///
    /// This sends the script to Redis using SCRIPT LOAD, which caches it
    /// for future EVALSHA calls.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig, Script};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let script = Script::new("return 'Hello, World!'");
    ///
    /// // Preload the script
    /// let sha = script.load(&client).await?;
    /// println!("Script loaded with SHA: {}", sha);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn load(&self, client: &crate::Client) -> RedisResult<String> {
        client.script_load(&self.source).await
    }
}

/// Script manager for caching and managing multiple scripts
#[derive(Debug)]
pub struct ScriptManager {
    scripts: Arc<RwLock<HashMap<String, Script>>>,
}

impl ScriptManager {
    /// Create a new script manager
    #[must_use]
    pub fn new() -> Self {
        Self {
            scripts: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Register a script with the manager
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use redis_oxide::{Script, ScriptManager};
    ///
    /// # #[tokio::main]
    /// # async fn main() {
    /// let mut manager = ScriptManager::new();
    /// let script = Script::new("return 'Hello'");
    ///
    /// manager.register("greeting", script).await;
    /// # }
    /// ```
    pub async fn register(&self, name: impl Into<String>, script: Script) {
        let mut scripts = self.scripts.write().await;
        scripts.insert(name.into(), script);
    }

    /// Get a script by name
    ///
    /// # Examples
    ///
    /// ```
    /// # use redis_oxide::{Script, ScriptManager};
    /// # #[tokio::main]
    /// # async fn main() {
    /// let manager = ScriptManager::new();
    /// let script = Script::new("return 'Hello'");
    /// manager.register("greeting", script).await;
    ///
    /// if let Some(script) = manager.get("greeting").await {
    ///     println!("Found script with SHA: {}", script.sha());
    /// }
    /// # }
    /// ```
    pub async fn get(&self, name: &str) -> Option<Script> {
        let scripts = self.scripts.read().await;
        scripts.get(name).cloned()
    }

    /// Execute a script by name
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig, Script, ScriptManager};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let manager = ScriptManager::new();
    /// let script = Script::new("return KEYS[1]");
    /// manager.register("get_key", script).await;
    ///
    /// let result: String = manager.execute(
    ///     "get_key",
    ///     &client,
    ///     vec!["mykey".to_string()],
    ///     vec![]
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn execute<T>(
        &self,
        name: &str,
        client: &crate::Client,
        keys: Vec<String>,
        args: Vec<String>,
    ) -> RedisResult<T>
    where
        T: TryFrom<RespValue>,
        T::Error: Into<RedisError>,
    {
        let script = self
            .get(name)
            .await
            .ok_or_else(|| RedisError::Protocol(format!("Script '{}' not found", name)))?;

        script.execute(client, keys, args).await
    }

    /// Load all registered scripts into Redis cache
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig, Script, ScriptManager};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let manager = ScriptManager::new();
    ///
    /// // Register some scripts
    /// manager.register("script1", Script::new("return 1")).await;
    /// manager.register("script2", Script::new("return 2")).await;
    ///
    /// // Load all scripts at once
    /// let results = manager.load_all(&client).await?;
    /// println!("Loaded {} scripts", results.len());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn load_all(&self, client: &crate::Client) -> RedisResult<HashMap<String, String>> {
        let scripts = self.scripts.read().await;
        let mut results = HashMap::new();

        for (name, script) in scripts.iter() {
            let sha = script.load(client).await?;
            results.insert(name.clone(), sha);
        }

        Ok(results)
    }

    /// Get the number of registered scripts
    #[must_use]
    pub async fn len(&self) -> usize {
        let scripts = self.scripts.read().await;
        scripts.len()
    }

    /// Check if the manager has any scripts
    #[must_use]
    pub async fn is_empty(&self) -> bool {
        let scripts = self.scripts.read().await;
        scripts.is_empty()
    }

    /// List all registered script names
    pub async fn list_scripts(&self) -> Vec<String> {
        let scripts = self.scripts.read().await;
        scripts.keys().cloned().collect()
    }

    /// Remove a script from the manager
    pub async fn remove(&self, name: &str) -> Option<Script> {
        let mut scripts = self.scripts.write().await;
        scripts.remove(name)
    }

    /// Clear all scripts from the manager
    pub async fn clear(&self) {
        let mut scripts = self.scripts.write().await;
        scripts.clear();
    }
}

impl Default for ScriptManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Calculate SHA1 hash of a string
fn calculate_sha1(input: &str) -> String {
    let mut hasher = Sha1::new();
    hasher.update(input.as_bytes());
    let result = hasher.finalize();
    hex::encode(result)
}

/// Common Lua script patterns
pub mod patterns {
    use super::Script;

    /// Atomic increment with expiration
    ///
    /// # Arguments
    /// - KEYS[1]: The key to increment
    /// - ARGV\[1\]: Increment amount
    /// - ARGV\[2\]: Expiration time in seconds
    pub fn atomic_increment_with_expiration() -> Script {
        Script::new(
            r"
            local key = KEYS[1]
            local increment = tonumber(ARGV[1])
            local expiration = tonumber(ARGV[2])
            
            local current = redis.call('GET', key)
            local new_value
            
            if current == false then
                new_value = increment
            else
                new_value = tonumber(current) + increment
            end
            
            redis.call('SET', key, new_value)
            redis.call('EXPIRE', key, expiration)
            
            return new_value
        ",
        )
    }

    /// Conditional set (SET if value matches)
    ///
    /// # Arguments
    /// - KEYS\[1\]: The key to set
    /// - ARGV\[1\]: Expected current value
    /// - ARGV\[2\]: New value to set
    pub fn conditional_set() -> Script {
        Script::new(
            r"
            local key = KEYS[1]
            local expected = ARGV[1]
            local new_value = ARGV[2]
            
            local current = redis.call('GET', key)
            
            if current == expected then
                redis.call('SET', key, new_value)
                return 1
            else
                return 0
            end
        ",
        )
    }

    /// Rate limiting with sliding window
    ///
    /// # Arguments
    /// - KEYS\[1\]: The rate limit key
    /// - ARGV\[1\]: Window size in seconds
    /// - ARGV\[2\]: Maximum requests per window
    pub fn sliding_window_rate_limit() -> Script {
        Script::new(
            r#"
            local key = KEYS[1]
            local window = tonumber(ARGV[1])
            local limit = tonumber(ARGV[2])
            local now = redis.call('TIME')[1]
            
            -- Remove old entries
            redis.call('ZREMRANGEBYSCORE', key, 0, now - window)
            
            -- Count current entries
            local current = redis.call('ZCARD', key)
            
            if current < limit then
                -- Add current request
                redis.call('ZADD', key, now, now)
                redis.call('EXPIRE', key, window)
                return { 1, limit - current - 1 }
            else
                return { 0, 0 }
            end
        "#,
        )
    }

    /// Distributed lock with expiration
    ///
    /// # Arguments
    /// - KEYS\[1\]: The lock key
    /// - ARGV\[1\]: Lock identifier (unique per client)
    /// - ARGV\[2\]: Lock expiration in seconds
    pub fn distributed_lock() -> Script {
        Script::new(
            r#"
            local key = KEYS[1]
            local identifier = ARGV[1]
            local expiration = tonumber(ARGV[2])
            
            if redis.call('SET', key, identifier, 'NX', 'EX', expiration) then
                return 1
            else
                return 0
            end
        "#,
        )
    }

    /// Release distributed lock
    ///
    /// # Arguments
    /// - KEYS\[1\]: The lock key
    /// - ARGV\[1\]: Lock identifier (must match)
    pub fn release_lock() -> Script {
        Script::new(
            r#"
            local key = KEYS[1]
            local identifier = ARGV[1]
            
            if redis.call('GET', key) == identifier then
                return redis.call('DEL', key)
            else
                return 0
            end
        "#,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_script_creation() {
        let script = Script::new("return 'hello'");
        assert_eq!(script.source(), "return 'hello'");
        assert!(!script.sha().is_empty());
        assert_eq!(script.sha().len(), 40); // SHA1 is 40 hex characters
    }

    #[test]
    fn test_sha1_calculation() {
        let sha = calculate_sha1("hello world");
        assert_eq!(sha, "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed");
    }

    #[test]
    fn test_script_sha_consistency() {
        let script1 = Script::new("return 1");
        let script2 = Script::new("return 1");
        assert_eq!(script1.sha(), script2.sha());
    }

    #[test]
    fn test_script_sha_uniqueness() {
        let script1 = Script::new("return 1");
        let script2 = Script::new("return 2");
        assert_ne!(script1.sha(), script2.sha());
    }

    #[tokio::test]
    async fn test_script_manager_creation() {
        let manager = ScriptManager::new();
        assert!(manager.is_empty().await);
        assert_eq!(manager.len().await, 0);
    }

    #[tokio::test]
    async fn test_script_manager_register_and_get() {
        let manager = ScriptManager::new();
        let script = Script::new("return 'test'");
        let sha = script.sha().to_string();

        manager.register("test_script", script).await;

        assert!(!manager.is_empty().await);
        assert_eq!(manager.len().await, 1);

        let retrieved = manager.get("test_script").await.unwrap();
        assert_eq!(retrieved.sha(), sha);
        assert_eq!(retrieved.source(), "return 'test'");
    }

    #[tokio::test]
    async fn test_script_manager_remove() {
        let manager = ScriptManager::new();
        let script = Script::new("return 'test'");

        manager.register("test_script", script).await;
        assert_eq!(manager.len().await, 1);

        let removed = manager.remove("test_script").await;
        assert!(removed.is_some());
        assert_eq!(manager.len().await, 0);

        let not_found = manager.remove("nonexistent").await;
        assert!(not_found.is_none());
    }

    #[tokio::test]
    async fn test_script_manager_clear() {
        let manager = ScriptManager::new();

        manager.register("script1", Script::new("return 1")).await;
        manager.register("script2", Script::new("return 2")).await;
        assert_eq!(manager.len().await, 2);

        manager.clear().await;
        assert_eq!(manager.len().await, 0);
        assert!(manager.is_empty().await);
    }

    #[tokio::test]
    async fn test_script_manager_list_scripts() {
        let manager = ScriptManager::new();

        manager
            .register("script_a", Script::new("return 'a'"))
            .await;
        manager
            .register("script_b", Script::new("return 'b'"))
            .await;

        let mut scripts = manager.list_scripts().await;
        scripts.sort();

        assert_eq!(scripts, vec!["script_a", "script_b"]);
    }

    #[test]
    fn test_pattern_scripts() {
        // Test that pattern scripts can be created without panicking
        let _increment = patterns::atomic_increment_with_expiration();
        let _conditional = patterns::conditional_set();
        let _rate_limit = patterns::sliding_window_rate_limit();
        let _lock = patterns::distributed_lock();
        let _unlock = patterns::release_lock();
    }
}