relay_redis/
scripts.rs

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
use redis::Script;
use std::sync::OnceLock;

/// A collection of static methods to load predefined Redis scripts.
pub struct RedisScripts;

impl RedisScripts {
    /// Returns all [`Script`]s.
    pub fn all() -> [&'static Script; 3] {
        [
            Self::load_cardinality(),
            Self::load_global_quota(),
            Self::load_is_rate_limited(),
        ]
    }

    /// Loads the cardinality Redis script.
    pub fn load_cardinality() -> &'static Script {
        static SCRIPT: OnceLock<Script> = OnceLock::new();
        SCRIPT.get_or_init(|| Script::new(include_str!("scripts/cardinality.lua")))
    }

    /// Loads the global quota Redis script.
    pub fn load_global_quota() -> &'static Script {
        static SCRIPT: OnceLock<Script> = OnceLock::new();
        SCRIPT.get_or_init(|| Script::new(include_str!("scripts/global_quota.lua")))
    }

    /// Loads the rate limiting check Redis script.
    pub fn load_is_rate_limited() -> &'static Script {
        static SCRIPT: OnceLock<Script> = OnceLock::new();
        SCRIPT.get_or_init(|| Script::new(include_str!("scripts/is_rate_limited.lua")))
    }
}