Skip to main content

pleme_redis/
lib.rs

1//! Pleme Redis - Redis patterns for the Pleme platform
2//!
3//! This library provides reusable Redis patterns for:
4//! - Presence management (online/offline status with TTL)
5//! - Typing indicators (ephemeral status)
6//! - Caching (with product-scoped keys)
7//! - Pub/Sub messaging
8//! - Distributed locking (for background jobs)
9//! - Job queue processing (FIFO queue with worker pool and retry logic)
10//!
11//! All patterns are multi-tenant safe with product scoping.
12
13pub mod error;
14pub mod presence;
15pub mod typing;
16pub mod cache;
17pub mod pubsub;
18pub mod locking;
19pub mod job;
20pub mod job_queue;
21pub mod worker_pool;
22
23// Re-export commonly used types
24pub use error::{RedisError, Result};
25pub use presence::PresenceManager;
26pub use typing::TypingManager;
27pub use cache::CacheManager;
28pub use pubsub::PubSubManager;
29pub use locking::{LockManager, LockGuard};
30pub use job::{Job, QueuedJob};
31pub use job_queue::{JobQueue, QueueError, QueueStats};
32pub use worker_pool::{
33    JobExecutor, JobStore, WorkerPool, WorkerPoolConfig,
34    WorkerError, StoreError, ExecutorError,
35};
36
37/// Configuration for Redis services
38#[derive(Debug, Clone)]
39pub struct RedisConfig {
40    /// Redis connection URL
41    pub url: String,
42
43    /// Presence TTL in seconds (default: 300 = 5 minutes)
44    pub presence_ttl_seconds: u64,
45
46    /// Typing indicator TTL in seconds (default: 5 seconds)
47    pub typing_ttl_seconds: u64,
48
49    /// Cache TTL in seconds (default: 300 = 5 minutes)
50    pub cache_ttl_seconds: u64,
51}
52
53impl Default for RedisConfig {
54    fn default() -> Self {
55        Self {
56            url: "redis://127.0.0.1:6379".to_string(),
57            presence_ttl_seconds: 300,
58            typing_ttl_seconds: 5,
59            cache_ttl_seconds: 300,
60        }
61    }
62}
63
64impl RedisConfig {
65    /// Create a new Redis configuration
66    pub fn new(url: impl Into<String>) -> Self {
67        Self {
68            url: url.into(),
69            ..Default::default()
70        }
71    }
72
73    /// Set presence TTL
74    pub fn with_presence_ttl(mut self, seconds: u64) -> Self {
75        self.presence_ttl_seconds = seconds;
76        self
77    }
78
79    /// Set typing TTL
80    pub fn with_typing_ttl(mut self, seconds: u64) -> Self {
81        self.typing_ttl_seconds = seconds;
82        self
83    }
84
85    /// Set cache TTL
86    pub fn with_cache_ttl(mut self, seconds: u64) -> Self {
87        self.cache_ttl_seconds = seconds;
88        self
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn test_config_builder() {
98        let config = RedisConfig::new("redis://localhost:6379")
99            .with_presence_ttl(600)
100            .with_typing_ttl(10)
101            .with_cache_ttl(1800);
102
103        assert_eq!(config.url, "redis://localhost:6379");
104        assert_eq!(config.presence_ttl_seconds, 600);
105        assert_eq!(config.typing_ttl_seconds, 10);
106        assert_eq!(config.cache_ttl_seconds, 1800);
107    }
108}