armature-cache 0.4.0

Cache management for Armature framework with Redis and in-memory support
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
//! Parallel batch operations for cache stores.

use crate::error::{CacheError, CacheResult};
use crate::traits::CacheStore;
use futures::StreamExt;
use futures::future::join_all;
use serde::{Serialize, de::DeserializeOwned};
use std::collections::HashMap;
use std::time::Duration;

/// Default cap on how many warm-up factories [`ParallelCacheOps::warm_cache`]
/// runs at once.
///
/// The factory is a user-supplied loader — typically a database query — so an
/// unbounded fan-out would turn a 10,000-key warm-up into 10,000 simultaneous
/// queries: precisely the stampede that
/// [`CacheManager::get_or_set`](crate::manager::CacheManager::get_or_set)'s
/// single-flight exists to prevent. Use
/// [`ParallelCacheOps::warm_cache_with_concurrency`] to pick a different limit.
pub const DEFAULT_WARM_CONCURRENCY: usize = 32;

/// Parallel batch operations for cache stores.
///
/// This module provides high-performance batch operations that execute
/// multiple cache operations concurrently, significantly reducing total latency.
///
/// # Performance
///
/// - **get_many**: 10-100x faster than sequential gets (depending on network latency)
/// - **set_many**: 10-100x faster than sequential sets
/// - **delete_many**: Similar performance gains
///
/// # Examples
///
/// ```no_run
/// use armature_cache::*;
/// use armature_cache::parallel::*;
///
/// # async fn example() -> CacheResult<()> {
/// let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;
///
/// // Get multiple keys in parallel
/// let keys = vec!["user:1", "user:2", "user:3"];
/// let values = get_many_json(&cache, &keys).await?;
///
/// // Set multiple keys in parallel
/// let items = vec![
///     ("key1", "value1".to_string()),
///     ("key2", "value2".to_string()),
/// ];
/// set_many_json(&cache, &items, None).await?;
/// # Ok(())
/// # }
/// ```
pub struct ParallelCacheOps;

impl ParallelCacheOps {
    /// Get multiple JSON values in parallel.
    ///
    /// # Arguments
    ///
    /// * `store` - The cache store
    /// * `keys` - Slice of keys to fetch
    ///
    /// # Returns
    ///
    /// A vector of optional values in the same order as keys.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use armature_cache::*;
    /// use armature_cache::parallel::ParallelCacheOps;
    ///
    /// # async fn example() -> CacheResult<()> {
    /// let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;
    ///
    /// let keys = vec!["key1", "key2", "key3"];
    /// let values = ParallelCacheOps::get_many_json(&cache, &keys).await?;
    ///
    /// for (key, value) in keys.iter().zip(values.iter()) {
    ///     println!("{}: {:?}", key, value);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_many_json<S: CacheStore>(
        store: &S,
        keys: &[&str],
    ) -> CacheResult<Vec<Option<String>>> {
        // Delegate to the store's native batch primitive. Backends like Redis
        // collapse this into a single `MGET` round-trip; others fall back to the
        // concurrent per-key loop. Order matches `keys` in both cases.
        store.mget(keys).await
    }

    /// Get multiple typed values in parallel.
    ///
    /// # Type Parameters
    ///
    /// * `T` - The type to deserialize into
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use armature_cache::*;
    /// use armature_cache::parallel::ParallelCacheOps;
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Serialize, Deserialize)]
    /// struct User {
    ///     id: u64,
    ///     name: String,
    /// }
    ///
    /// # async fn example() -> CacheResult<()> {
    /// let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;
    ///
    /// let keys = vec!["user:1", "user:2", "user:3"];
    /// let users: Vec<Option<User>> = ParallelCacheOps::get_many(&cache, &keys).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_many<S: CacheStore, T: DeserializeOwned>(
        store: &S,
        keys: &[&str],
    ) -> CacheResult<Vec<Option<T>>> {
        let json_values = Self::get_many_json(store, keys).await?;

        json_values
            .into_iter()
            .map(|opt_json| {
                opt_json
                    .map(|json| {
                        serde_json::from_str(&json)
                            .map_err(|e| CacheError::Deserialization(e.to_string()))
                    })
                    .transpose()
            })
            .collect()
    }

    /// Set multiple JSON values in parallel.
    ///
    /// # Arguments
    ///
    /// * `store` - The cache store
    /// * `items` - Slice of (key, value) tuples
    /// * `ttl` - Optional time-to-live for all items
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use armature_cache::*;
    /// use armature_cache::parallel::ParallelCacheOps;
    /// use std::time::Duration;
    ///
    /// # async fn example() -> CacheResult<()> {
    /// let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;
    ///
    /// let items = vec![
    ///     ("key1", r#"{"value": 1}"#.to_string()),
    ///     ("key2", r#"{"value": 2}"#.to_string()),
    /// ];
    ///
    /// ParallelCacheOps::set_many_json(&cache, &items, Some(Duration::from_secs(3600))).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn set_many_json<S: CacheStore>(
        store: &S,
        items: &[(&str, String)],
        ttl: Option<Duration>,
    ) -> CacheResult<()> {
        // Delegate to the store's native batch primitive (e.g. Redis `MSET` /
        // pipelined `SET ... EX`), falling back to the per-key loop otherwise.
        store.mset(items, ttl).await
    }

    /// Set multiple typed values in parallel.
    ///
    /// # Type Parameters
    ///
    /// * `T` - The type to serialize from
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use armature_cache::*;
    /// use armature_cache::parallel::ParallelCacheOps;
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Serialize, Deserialize)]
    /// struct Counter {
    ///     count: u64,
    /// }
    ///
    /// # async fn example() -> CacheResult<()> {
    /// let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;
    ///
    /// let items = vec![
    ///     ("counter:1", Counter { count: 10 }),
    ///     ("counter:2", Counter { count: 20 }),
    /// ];
    ///
    /// ParallelCacheOps::set_many(&cache, &items, None).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn set_many<S: CacheStore, T: Serialize>(
        store: &S,
        items: &[(&str, T)],
        ttl: Option<Duration>,
    ) -> CacheResult<()> {
        let json_items: Result<Vec<_>, _> = items
            .iter()
            .map(|(key, value)| {
                serde_json::to_string(value)
                    .map(|json| (*key, json))
                    .map_err(|e| CacheError::Serialization(e.to_string()))
            })
            .collect();

        let json_items = json_items?;
        let item_refs: Vec<_> = json_items.iter().map(|(k, v)| (*k, v.clone())).collect();

        Self::set_many_json(store, &item_refs, ttl).await
    }

    /// Delete multiple keys in parallel.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use armature_cache::*;
    /// use armature_cache::parallel::ParallelCacheOps;
    ///
    /// # async fn example() -> CacheResult<()> {
    /// let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;
    ///
    /// let keys = vec!["key1", "key2", "key3"];
    /// ParallelCacheOps::delete_many(&cache, &keys).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn delete_many<S: CacheStore>(store: &S, keys: &[&str]) -> CacheResult<()> {
        // Delegate to the store's native batch primitive (e.g. Redis variadic
        // `DEL`), falling back to the concurrent per-key loop otherwise.
        store.mdel(keys).await
    }

    /// Check if multiple keys exist in parallel.
    ///
    /// # Returns
    ///
    /// A vector of booleans indicating existence, in the same order as keys.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use armature_cache::*;
    /// use armature_cache::parallel::ParallelCacheOps;
    ///
    /// # async fn example() -> CacheResult<()> {
    /// let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;
    ///
    /// let keys = vec!["key1", "key2", "key3"];
    /// let exists = ParallelCacheOps::exists_many(&cache, &keys).await?;
    ///
    /// for (key, exists) in keys.iter().zip(exists.iter()) {
    ///     println!("{}: {}", key, exists);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn exists_many<S: CacheStore>(store: &S, keys: &[&str]) -> CacheResult<Vec<bool>> {
        let futures = keys.iter().map(|key| store.exists(key));
        let results: Vec<CacheResult<bool>> = join_all(futures).await;

        results.into_iter().collect()
    }

    /// Get TTL for multiple keys in parallel.
    ///
    /// # Returns
    ///
    /// A vector of optional durations, in the same order as keys.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use armature_cache::*;
    /// use armature_cache::parallel::ParallelCacheOps;
    ///
    /// # async fn example() -> CacheResult<()> {
    /// let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;
    ///
    /// let keys = vec!["key1", "key2", "key3"];
    /// let ttls = ParallelCacheOps::ttl_many(&cache, &keys).await?;
    ///
    /// for (key, ttl) in keys.iter().zip(ttls.iter()) {
    ///     println!("{}: {:?}", key, ttl);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn ttl_many<S: CacheStore>(
        store: &S,
        keys: &[&str],
    ) -> CacheResult<Vec<Option<Duration>>> {
        let futures = keys.iter().map(|key| store.ttl(key));
        let results: Vec<CacheResult<Option<Duration>>> = join_all(futures).await;

        results.into_iter().collect()
    }

    /// Cache warming: preload multiple keys into cache.
    ///
    /// # Concurrency
    ///
    /// At most [`DEFAULT_WARM_CONCURRENCY`] factories run at a time. The
    /// factory is the caller's loader (usually a DB fetch), so warming a large
    /// key set without a bound would fire one query per key simultaneously.
    /// Use [`Self::warm_cache_with_concurrency`] to choose the limit.
    ///
    /// # Type Parameters
    ///
    /// * `T` - The type to serialize
    /// * `F` - Factory function that returns data for a given key
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use armature_cache::*;
    /// use armature_cache::parallel::ParallelCacheOps;
    /// use std::time::Duration;
    ///
    /// # async fn example() -> CacheResult<()> {
    /// let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;
    ///
    /// let keys = vec!["user:1", "user:2", "user:3"];
    ///
    /// ParallelCacheOps::warm_cache(
    ///     &cache,
    ///     &keys,
    ///     Some(Duration::from_secs(3600)),
    ///     |key: &str| async move {
    ///         // Fetch from database
    ///         let data = format!("Data for {}", key);
    ///         Ok::<String, CacheError>(data)
    ///     },
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn warm_cache<S, T, F, Fut>(
        store: &S,
        keys: &[&str],
        ttl: Option<Duration>,
        factory: F,
    ) -> CacheResult<()>
    where
        S: CacheStore,
        T: Serialize,
        F: Fn(&str) -> Fut,
        Fut: std::future::Future<Output = CacheResult<T>>,
    {
        Self::warm_cache_with_concurrency(store, keys, ttl, DEFAULT_WARM_CONCURRENCY, factory).await
    }

    /// Cache warming with an explicit cap on concurrent factory invocations.
    ///
    /// `max_concurrent` is the number of factories (and their follow-up
    /// writes) allowed to be in flight at once; `0` is treated as `1`. See
    /// [`DEFAULT_WARM_CONCURRENCY`] for why the fan-out is bounded at all.
    ///
    /// The first error aborts the warm-up: keys already written stay written,
    /// and the remaining ones are not attempted.
    pub async fn warm_cache_with_concurrency<S, T, F, Fut>(
        store: &S,
        keys: &[&str],
        ttl: Option<Duration>,
        max_concurrent: usize,
        factory: F,
    ) -> CacheResult<()>
    where
        S: CacheStore,
        T: Serialize,
        F: Fn(&str) -> Fut,
        Fut: std::future::Future<Output = CacheResult<T>>,
    {
        let limit = max_concurrent.max(1);
        let factory = &factory;

        let mut warmed = futures::stream::iter(keys.iter().map(|key| async move {
            let value = factory(key).await?;
            let json = serde_json::to_string(&value)
                .map_err(|e| CacheError::Serialization(e.to_string()))?;
            store.set_json(key, json, ttl).await?;
            Ok::<(), CacheError>(())
        }))
        .buffer_unordered(limit);

        while let Some(result) = warmed.next().await {
            result?;
        }

        Ok(())
    }
}

/// Helper functions for parallel cache operations.
///
/// These functions provide a more convenient API than `ParallelCacheOps` methods.
/// Get multiple JSON values in parallel.
pub async fn get_many_json<S: CacheStore>(
    store: &S,
    keys: &[&str],
) -> CacheResult<Vec<Option<String>>> {
    ParallelCacheOps::get_many_json(store, keys).await
}

/// Get multiple typed values in parallel.
pub async fn get_many<S: CacheStore, T: DeserializeOwned>(
    store: &S,
    keys: &[&str],
) -> CacheResult<Vec<Option<T>>> {
    ParallelCacheOps::get_many(store, keys).await
}

/// Set multiple JSON values in parallel.
pub async fn set_many_json<S: CacheStore>(
    store: &S,
    items: &[(&str, String)],
    ttl: Option<Duration>,
) -> CacheResult<()> {
    ParallelCacheOps::set_many_json(store, items, ttl).await
}

/// Set multiple typed values in parallel.
pub async fn set_many<S: CacheStore, T: Serialize>(
    store: &S,
    items: &[(&str, T)],
    ttl: Option<Duration>,
) -> CacheResult<()> {
    ParallelCacheOps::set_many(store, items, ttl).await
}

/// Delete multiple keys in parallel.
pub async fn delete_many<S: CacheStore>(store: &S, keys: &[&str]) -> CacheResult<()> {
    ParallelCacheOps::delete_many(store, keys).await
}

/// Build a HashMap from multiple keys fetched in parallel.
pub async fn get_many_as_map<S: CacheStore, T: DeserializeOwned>(
    store: &S,
    keys: &[&str],
) -> CacheResult<HashMap<String, T>> {
    let values = get_many(store, keys).await?;

    let map: HashMap<String, T> = keys
        .iter()
        .zip(values)
        .filter_map(|(key, opt_value)| opt_value.map(|value| (key.to_string(), value)))
        .collect();

    Ok(map)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tiered::InMemoryCache;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    /// Tracks how many factory invocations are in flight simultaneously.
    #[derive(Default)]
    struct ConcurrencyProbe {
        current: AtomicUsize,
        peak: AtomicUsize,
        total: AtomicUsize,
    }

    impl ConcurrencyProbe {
        fn enter(&self) {
            let current = self.current.fetch_add(1, Ordering::SeqCst) + 1;
            self.total.fetch_add(1, Ordering::SeqCst);
            self.peak.fetch_max(current, Ordering::SeqCst);
        }

        fn leave(&self) {
            self.current.fetch_sub(1, Ordering::SeqCst);
        }
    }

    /// Regression: `warm_cache` used to `try_join_all` one future per key with
    /// no limit, so warming N keys ran N user factories (documented as DB
    /// fetches) simultaneously. The fan-out must now be bounded.
    #[tokio::test]
    async fn test_warm_cache_bounds_factory_concurrency() {
        let cache = InMemoryCache::new();
        let probe = Arc::new(ConcurrencyProbe::default());
        let keys: Vec<String> = (0..64).map(|i| format!("k{i}")).collect();
        let key_refs: Vec<&str> = keys.iter().map(|k| k.as_str()).collect();

        const LIMIT: usize = 4;

        ParallelCacheOps::warm_cache_with_concurrency(
            &cache,
            &key_refs,
            None,
            LIMIT,
            |key: &str| {
                let probe = probe.clone();
                let key = key.to_string();
                async move {
                    probe.enter();
                    // Suspend so the other buffered futures get a chance to run
                    // and the peak reflects real overlap.
                    for _ in 0..3 {
                        tokio::task::yield_now().await;
                    }
                    probe.leave();
                    Ok::<String, CacheError>(format!("value-for-{key}"))
                }
            },
        )
        .await
        .unwrap();

        assert_eq!(probe.total.load(Ordering::SeqCst), 64);
        assert!(
            probe.peak.load(Ordering::SeqCst) <= LIMIT,
            "warm_cache ran {} factories at once, limit was {LIMIT}",
            probe.peak.load(Ordering::SeqCst)
        );

        // Every key was still warmed.
        assert_eq!(
            cache.get_json("k0").await.unwrap(),
            Some("\"value-for-k0\"".to_string())
        );
        assert_eq!(
            cache.get_json("k63").await.unwrap(),
            Some("\"value-for-k63\"".to_string())
        );
    }

    /// The default entry point applies [`DEFAULT_WARM_CONCURRENCY`] rather
    /// than fanning out over every key.
    #[tokio::test]
    async fn test_warm_cache_default_limit_applies() {
        let cache = InMemoryCache::new();
        let probe = Arc::new(ConcurrencyProbe::default());
        let keys: Vec<String> = (0..DEFAULT_WARM_CONCURRENCY * 4)
            .map(|i| format!("k{i}"))
            .collect();
        let key_refs: Vec<&str> = keys.iter().map(|k| k.as_str()).collect();

        ParallelCacheOps::warm_cache(&cache, &key_refs, None, |_key: &str| {
            let probe = probe.clone();
            async move {
                probe.enter();
                for _ in 0..3 {
                    tokio::task::yield_now().await;
                }
                probe.leave();
                Ok::<u32, CacheError>(1)
            }
        })
        .await
        .unwrap();

        assert!(probe.peak.load(Ordering::SeqCst) <= DEFAULT_WARM_CONCURRENCY);
        assert_eq!(
            probe.total.load(Ordering::SeqCst),
            DEFAULT_WARM_CONCURRENCY * 4
        );
    }

    /// A concurrency limit of 0 is clamped to 1 rather than deadlocking or
    /// silently doing nothing.
    #[tokio::test]
    async fn test_warm_cache_zero_concurrency_is_clamped_to_one() {
        let cache = InMemoryCache::new();
        let probe = Arc::new(ConcurrencyProbe::default());

        ParallelCacheOps::warm_cache_with_concurrency(
            &cache,
            &["a", "b", "c"],
            None,
            0,
            |_key: &str| {
                let probe = probe.clone();
                async move {
                    probe.enter();
                    tokio::task::yield_now().await;
                    probe.leave();
                    Ok::<u32, CacheError>(7)
                }
            },
        )
        .await
        .unwrap();

        assert_eq!(probe.peak.load(Ordering::SeqCst), 1);
        assert_eq!(probe.total.load(Ordering::SeqCst), 3);
        assert_eq!(cache.get_json("c").await.unwrap(), Some("7".to_string()));
    }

    /// A factory failure aborts the warm-up and surfaces the error.
    #[tokio::test]
    async fn test_warm_cache_propagates_factory_error() {
        let cache = InMemoryCache::new();

        let result = ParallelCacheOps::warm_cache_with_concurrency(
            &cache,
            &["a", "b"],
            None,
            1,
            |key: &str| {
                let fails = key == "b";
                async move {
                    if fails {
                        Err(CacheError::Other("factory failed".to_string()))
                    } else {
                        Ok(1_u32)
                    }
                }
            },
        )
        .await;

        assert!(result.is_err());
    }
}