prax-query 0.11.0

Type-safe query builder for the Prax ORM
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
//! Redis cache backend for distributed caching.
//!
//! > **Status: not yet implemented.** No Redis client is compiled into this
//! > crate, so this backend is a stub that fails loudly instead of silently
//! > succeeding:
//! >
//! > - [`RedisConnection::new`] / [`RedisCache::new`] return an error at
//! >   construction time.
//! > - Every command (`get`, `set`, `del`, `scan`, `mget`, pipeline
//! >   execution, …) returns [`CacheError::Backend`] with a
//! >   "redis backend not available" message.
//!
//! A real implementation — with connection pooling (bb8/deadpool), cluster
//! support, pipelining, Lua scripting, and Pub/Sub invalidation — requires
//! adding a Redis client dependency (e.g. `redis-rs` or `fred`) and is
//! planned for a future release.
//!
//! # Example
//!
//! ```rust,ignore
//! use prax_query::data_cache::redis::{RedisCache, RedisCacheConfig};
//!
//! // Currently returns Err(CacheError::Backend) — the backend is not implemented.
//! let cache = RedisCache::new(RedisCacheConfig {
//!     url: "redis://localhost:6379".to_string(),
//!     pool_size: 10,
//!     ..Default::default()
//! }).await?;
//! ```

use std::time::Duration;

use super::backend::{BackendStats, CacheBackend, CacheError, CacheResult};
use super::invalidation::EntityTag;
use super::key::{CacheKey, KeyPattern};

/// Error returned by every Redis operation: no Redis client is compiled in.
fn unavailable() -> CacheError {
    CacheError::Backend("redis backend not available: no redis client compiled in".to_string())
}

/// Configuration for Redis cache.
#[derive(Debug, Clone)]
pub struct RedisCacheConfig {
    /// Redis connection URL.
    pub url: String,
    /// Connection pool size.
    pub pool_size: u32,
    /// Connection timeout.
    pub connection_timeout: Duration,
    /// Command timeout.
    pub command_timeout: Duration,
    /// Key prefix for all entries.
    pub key_prefix: String,
    /// Default TTL.
    pub default_ttl: Option<Duration>,
    /// Enable cluster mode.
    pub cluster_mode: bool,
    /// Database number (0-15).
    pub database: u8,
    /// Enable TLS.
    pub tls: bool,
    /// Username for AUTH.
    pub username: Option<String>,
    /// Password for AUTH.
    pub password: Option<String>,
}

impl Default for RedisCacheConfig {
    fn default() -> Self {
        Self {
            url: "redis://localhost:6379".to_string(),
            pool_size: 10,
            connection_timeout: Duration::from_secs(5),
            command_timeout: Duration::from_secs(2),
            key_prefix: "prax:cache".to_string(),
            default_ttl: Some(Duration::from_secs(300)),
            cluster_mode: false,
            database: 0,
            tls: false,
            username: None,
            password: None,
        }
    }
}

impl RedisCacheConfig {
    /// Create a new config with the given URL.
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            ..Default::default()
        }
    }

    /// Set pool size.
    pub fn with_pool_size(mut self, size: u32) -> Self {
        self.pool_size = size;
        self
    }

    /// Set key prefix.
    pub fn with_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.key_prefix = prefix.into();
        self
    }

    /// Set default TTL.
    pub fn with_ttl(mut self, ttl: Duration) -> Self {
        self.default_ttl = Some(ttl);
        self
    }

    /// Enable cluster mode.
    pub fn cluster(mut self) -> Self {
        self.cluster_mode = true;
        self
    }

    /// Set database number.
    pub fn database(mut self, db: u8) -> Self {
        self.database = db;
        self
    }

    /// Set authentication.
    pub fn auth(mut self, username: Option<String>, password: impl Into<String>) -> Self {
        self.username = username;
        self.password = Some(password.into());
        self
    }

    /// Build the full key with prefix.
    fn full_key(&self, key: &CacheKey) -> String {
        format!("{}:{}", self.key_prefix, key.as_str())
    }
}

/// Represents a Redis connection (placeholder for actual implementation).
///
/// **Not yet implemented:** no Redis client is compiled in. Construction via
/// [`RedisConnection::new`] fails, and every command returns
/// [`CacheError::Backend`]. A real implementation would use the `redis-rs`
/// or `fred` crate.
#[derive(Clone)]
pub struct RedisConnection {
    config: RedisCacheConfig,
    // In real impl: pool: Pool<RedisConnectionManager>
}

impl RedisConnection {
    /// Create a new connection.
    ///
    /// **Not yet implemented:** always returns [`CacheError::Backend`] —
    /// no Redis client is compiled in. A real implementation would create a
    /// connection pool (bb8/deadpool), establish initial connections, and
    /// verify connectivity.
    pub async fn new(config: RedisCacheConfig) -> CacheResult<Self> {
        let _ = config;
        Err(unavailable())
    }

    /// Get the config.
    pub fn config(&self) -> &RedisCacheConfig {
        &self.config
    }

    /// Execute a Redis command (not implemented — always errors).
    async fn execute<T>(&self, _cmd: &str, _args: &[&str]) -> CacheResult<T>
    where
        T: Default,
    {
        // Not implemented: no redis client compiled in.
        // Example with redis-rs:
        // let mut conn = self.pool.get().await?;
        // redis::cmd(cmd).arg(args).query_async(&mut *conn).await
        Err(unavailable())
    }

    /// GET command (not implemented — always errors).
    pub async fn get(&self, key: &str) -> CacheResult<Option<Vec<u8>>> {
        let _ = key;
        Err(unavailable())
    }

    /// SET command with optional TTL (not implemented — always errors).
    pub async fn set(&self, key: &str, value: &[u8], ttl: Option<Duration>) -> CacheResult<()> {
        let _ = (key, value, ttl);
        Err(unavailable())
    }

    /// DEL command (not implemented — always errors).
    pub async fn del(&self, key: &str) -> CacheResult<bool> {
        let _ = key;
        Err(unavailable())
    }

    /// EXISTS command (not implemented — always errors).
    pub async fn exists(&self, key: &str) -> CacheResult<bool> {
        let _ = key;
        Err(unavailable())
    }

    /// KEYS command (not implemented — always errors; a real impl would use SCAN in production).
    pub async fn keys(&self, pattern: &str) -> CacheResult<Vec<String>> {
        let _ = pattern;
        Err(unavailable())
    }

    /// MGET command (not implemented — always errors).
    pub async fn mget(&self, keys: &[String]) -> CacheResult<Vec<Option<Vec<u8>>>> {
        let _ = keys;
        Err(unavailable())
    }

    /// MSET command (not implemented — always errors).
    pub async fn mset(&self, pairs: &[(String, Vec<u8>)]) -> CacheResult<()> {
        let _ = pairs;
        Err(unavailable())
    }

    /// FLUSHDB command (not implemented — always errors).
    pub async fn flush(&self) -> CacheResult<()> {
        Err(unavailable())
    }

    /// DBSIZE command (not implemented — always errors).
    pub async fn dbsize(&self) -> CacheResult<usize> {
        Err(unavailable())
    }

    /// INFO command (not implemented — always errors).
    pub async fn info(&self) -> CacheResult<String> {
        Err(unavailable())
    }

    /// SCAN for pattern matching (not implemented — always errors).
    pub async fn scan(&self, pattern: &str, count: usize) -> CacheResult<Vec<String>> {
        let _ = (pattern, count);
        Err(unavailable())
    }

    /// Pipeline multiple commands.
    pub fn pipeline(&self) -> RedisPipeline {
        RedisPipeline::new(self.clone())
    }
}

/// A Redis pipeline for batching commands.
pub struct RedisPipeline {
    conn: RedisConnection,
    commands: Vec<PipelineCommand>,
}

enum PipelineCommand {
    Get(String),
    Set(String, Vec<u8>, Option<Duration>),
    Del(String),
}

impl RedisPipeline {
    fn new(conn: RedisConnection) -> Self {
        Self {
            conn,
            commands: Vec::new(),
        }
    }

    /// Add a GET command.
    pub fn get(mut self, key: impl Into<String>) -> Self {
        self.commands.push(PipelineCommand::Get(key.into()));
        self
    }

    /// Add a SET command.
    pub fn set(mut self, key: impl Into<String>, value: Vec<u8>, ttl: Option<Duration>) -> Self {
        self.commands
            .push(PipelineCommand::Set(key.into(), value, ttl));
        self
    }

    /// Add a DEL command.
    pub fn del(mut self, key: impl Into<String>) -> Self {
        self.commands.push(PipelineCommand::Del(key.into()));
        self
    }

    /// Execute the pipeline (not implemented — always errors).
    pub async fn execute(self) -> CacheResult<Vec<PipelineResult>> {
        // Not implemented: no redis client compiled in.
        Err(unavailable())
    }
}

/// Result of a pipeline command.
#[derive(Debug, Clone)]
pub enum PipelineResult {
    Ok,
    Value(Option<Vec<u8>>),
    Error(String),
}

/// Redis cache backend.
///
/// **Not yet implemented:** [`RedisCache::new`] fails at construction and
/// every [`CacheBackend`] operation returns [`CacheError::Backend`] — nothing
/// is ever silently stored, read, or invalidated. See the module-level docs.
#[derive(Clone)]
pub struct RedisCache {
    conn: RedisConnection,
    config: RedisCacheConfig,
}

impl RedisCache {
    /// Create a new Redis cache.
    ///
    /// **Not yet implemented:** always returns [`CacheError::Backend`] —
    /// no Redis client is compiled in.
    pub async fn new(config: RedisCacheConfig) -> CacheResult<Self> {
        let conn = RedisConnection::new(config.clone()).await?;
        Ok(Self { conn, config })
    }

    /// Create from a URL.
    ///
    /// **Not yet implemented:** always returns [`CacheError::Backend`].
    pub async fn from_url(url: &str) -> CacheResult<Self> {
        Self::new(RedisCacheConfig::new(url)).await
    }

    /// Get the connection.
    pub fn connection(&self) -> &RedisConnection {
        &self.conn
    }

    /// Get the config.
    pub fn config(&self) -> &RedisCacheConfig {
        &self.config
    }

    /// Build the full key with prefix.
    fn full_key(&self, key: &CacheKey) -> String {
        self.config.full_key(key)
    }
}

impl CacheBackend for RedisCache {
    async fn get<T>(&self, key: &CacheKey) -> CacheResult<Option<T>>
    where
        T: serde::de::DeserializeOwned,
    {
        let full_key = self.full_key(key);

        match self.conn.get(&full_key).await? {
            Some(data) => {
                let value: T = serde_json::from_slice(&data)
                    .map_err(|e| CacheError::Deserialization(e.to_string()))?;
                Ok(Some(value))
            }
            None => Ok(None),
        }
    }

    async fn set<T>(&self, key: &CacheKey, value: &T, ttl: Option<Duration>) -> CacheResult<()>
    where
        T: serde::Serialize + Sync,
    {
        let full_key = self.full_key(key);
        let data =
            serde_json::to_vec(value).map_err(|e| CacheError::Serialization(e.to_string()))?;

        let effective_ttl = ttl.or(self.config.default_ttl);
        self.conn.set(&full_key, &data, effective_ttl).await
    }

    async fn delete(&self, key: &CacheKey) -> CacheResult<bool> {
        let full_key = self.full_key(key);
        self.conn.del(&full_key).await
    }

    async fn exists(&self, key: &CacheKey) -> CacheResult<bool> {
        let full_key = self.full_key(key);
        self.conn.exists(&full_key).await
    }

    async fn get_many<T>(&self, keys: &[CacheKey]) -> CacheResult<Vec<Option<T>>>
    where
        T: serde::de::DeserializeOwned,
    {
        let full_keys: Vec<String> = keys.iter().map(|k| self.full_key(k)).collect();
        let results = self.conn.mget(&full_keys).await?;

        results
            .into_iter()
            .map(|opt| {
                opt.map(|data| {
                    serde_json::from_slice(&data)
                        .map_err(|e| CacheError::Deserialization(e.to_string()))
                })
                .transpose()
            })
            .collect()
    }

    async fn invalidate_pattern(&self, pattern: &KeyPattern) -> CacheResult<u64> {
        let full_pattern = format!("{}:{}", self.config.key_prefix, pattern.to_redis_pattern());

        // Use SCAN to find matching keys
        let keys = self.conn.scan(&full_pattern, 1000).await?;

        if keys.is_empty() {
            return Ok(0);
        }

        // Delete in batches
        let mut deleted = 0u64;
        for key in keys {
            if self.conn.del(&key).await? {
                deleted += 1;
            }
        }

        Ok(deleted)
    }

    async fn invalidate_tags(&self, tags: &[EntityTag]) -> CacheResult<u64> {
        // Not implemented: no redis client compiled in.
        // Real impl: tags stored as sets (tag:<tag_value> -> [key1, ...]);
        // SMEMBERS to get keys, then DEL.
        let _ = tags;
        Err(unavailable())
    }

    async fn clear(&self) -> CacheResult<()> {
        // In production, use SCAN + DEL with prefix
        // FLUSHDB would clear everything
        self.conn.flush().await
    }

    async fn len(&self) -> CacheResult<usize> {
        self.conn.dbsize().await
    }

    async fn stats(&self) -> CacheResult<BackendStats> {
        let info = self.conn.info().await?;
        let entries = self.conn.dbsize().await?;

        Ok(BackendStats {
            entries,
            memory_bytes: None, // Parse from INFO
            connections: Some(self.config.pool_size as usize),
            info: Some(info),
        })
    }
}

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

    #[test]
    fn test_redis_config() {
        let config = RedisCacheConfig::new("redis://localhost:6379")
            .with_pool_size(20)
            .with_prefix("myapp")
            .with_ttl(Duration::from_secs(600));

        assert_eq!(config.pool_size, 20);
        assert_eq!(config.key_prefix, "myapp");
        assert_eq!(config.default_ttl, Some(Duration::from_secs(600)));
    }

    #[test]
    fn test_full_key() {
        let config = RedisCacheConfig::new("redis://localhost").with_prefix("app:cache");

        let key = CacheKey::new("User", "id:123");
        let full = config.full_key(&key);

        assert_eq!(full, "app:cache:prax:User:id:123");
    }

    #[tokio::test]
    async fn test_redis_cache_creation() {
        // The Redis backend is not implemented: construction fails and every
        // operation errors instead of silently succeeding. Real integration
        // tests would need a Redis instance and a compiled-in client.
        let config = RedisCacheConfig::default();

        let err = RedisCache::new(config.clone())
            .await
            .err()
            .expect("redis construction should fail: no client compiled in");
        match &err {
            CacheError::Backend(msg) => assert!(msg.contains("not available")),
            other => panic!("expected backend error, got {other:?}"),
        }

        // Build directly (same-module access to private fields) to verify a
        // constructed instance also errors loudly on use.
        let conn = RedisConnection {
            config: config.clone(),
        };
        let cache = RedisCache { conn, config };

        assert_eq!(cache.config().pool_size, 10);

        let key = CacheKey::new("test", "key");
        assert!(cache.set(&key, &"value", None).await.is_err());
        let value: CacheResult<Option<String>> = cache.get(&key).await;
        assert!(value.is_err());
    }
}