Skip to main content

relay_redis/
scripts.rs

1use redis::Script;
2use std::sync::OnceLock;
3
4/// A collection of static methods to load predefined Redis scripts.
5pub struct RedisScripts;
6
7impl RedisScripts {
8    /// Returns all [`Script`]s.
9    pub fn all() -> [&'static Script; 2] {
10        [Self::load_global_quota(), Self::load_is_rate_limited()]
11    }
12
13    /// Loads the global quota Redis script.
14    pub fn load_global_quota() -> &'static Script {
15        static SCRIPT: OnceLock<Script> = OnceLock::new();
16        SCRIPT.get_or_init(|| Script::new(include_str!("scripts/global_quota.lua")))
17    }
18
19    /// Loads the rate limiting check Redis script.
20    pub fn load_is_rate_limited() -> &'static Script {
21        static SCRIPT: OnceLock<Script> = OnceLock::new();
22        SCRIPT.get_or_init(|| Script::new(include_str!("scripts/is_rate_limited.lua")))
23    }
24}