dioxus-provider 0.2.1

Data fetching and caching library for Dioxus applications with intelligent caching strategies and global providers.
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//! # Cache Management for dioxus-provider
//!
//! This module implements a global, type-erased cache for provider results, supporting:
//! - **Expiration**: Entries are removed after a configurable TTL.
//! - **Staleness (SWR)**: Entries can be marked stale and revalidated in the background.
//! - **LRU Eviction**: Least-recently-used entries are evicted to maintain a size limit.
//! - **Access/Usage Stats**: Provides statistics for cache introspection and tuning.
//!
//! ## Example
//! ```rust,no_run
//! use dioxus_provider::cache::ProviderCache;
//! let cache = ProviderCache::new();
//! cache.set("my_key".to_string(), 42);
//! let value: Option<i32> = cache.get("my_key");
//! ```
//! Cache management and async state types for dioxus-provider

use std::{
    any::Any,
    collections::HashMap,
    sync::{
        Arc, Mutex,
        atomic::{AtomicU32, Ordering},
    },
    time::Duration,
};

use crate::platform::{DEFAULT_MAX_CACHE_SIZE, DEFAULT_UNUSED_THRESHOLD};

// Platform-specific time imports
#[cfg(not(target_family = "wasm"))]
use std::time::Instant;
#[cfg(target_family = "wasm")]
use web_time::Instant;

/// Options for cache retrieval operations
#[derive(Debug, Clone, Default)]
pub struct CacheGetOptions {
    /// Optional expiration duration - entries older than this will be removed
    pub expiration: Option<Duration>,
    /// Optional stale time - used to check if data is stale
    pub stale_time: Option<Duration>,
    /// Whether to return staleness information
    pub check_staleness: bool,
}

impl CacheGetOptions {
    /// Create new cache get options with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the expiration duration
    pub fn with_expiration(mut self, expiration: Duration) -> Self {
        self.expiration = Some(expiration);
        self
    }

    /// Set the stale time
    pub fn with_stale_time(mut self, stale_time: Duration) -> Self {
        self.stale_time = Some(stale_time);
        self.check_staleness = true;
        self
    }

    /// Enable staleness checking
    pub fn check_staleness(mut self) -> Self {
        self.check_staleness = true;
        self
    }
}

/// Result type for cache get operations with staleness information
#[derive(Debug, Clone)]
pub struct CacheGetResult<T> {
    /// The cached data
    pub data: T,
    /// Whether the data is considered stale
    pub is_stale: bool,
}

/// A type-erased cache entry for storing provider results with timestamp and access tracking
#[derive(Clone)]
pub struct CacheEntry {
    data: Arc<dyn Any + Send + Sync>,
    cached_at: Arc<Mutex<Instant>>,
    last_accessed: Arc<Mutex<Instant>>,
    access_count: Arc<AtomicU32>,
}

impl CacheEntry {
    /// Creates a new cache entry with the given data.
    ///
    /// # Arguments
    ///
    /// * `data` - The data to cache.
    ///
    /// # Returns
    ///
    /// A new `CacheEntry` instance.
    pub fn new<T: Clone + Send + Sync + 'static>(data: T) -> Self {
        let now = Instant::now();
        Self {
            data: Arc::new(data),
            cached_at: Arc::new(Mutex::new(now)),
            last_accessed: Arc::new(Mutex::new(now)),
            access_count: Arc::new(AtomicU32::new(0)),
        }
    }

    /// Retrieves the cached data of type `T`.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `CacheEntry`.
    ///
    /// # Returns
    ///
    /// An `Option<T>` containing the cached data if available, or `None` if the entry is expired or not found.
    ///
    /// # Side Effects
    ///
    /// Updates the `last_accessed` timestamp and increments the `access_count`.
    pub fn get<T: Clone + Send + Sync + 'static>(&self) -> Option<T> {
        // Update last accessed time and access count
        if let Ok(mut last_accessed) = self.last_accessed.lock() {
            *last_accessed = Instant::now();
        }
        self.access_count.fetch_add(1, Ordering::SeqCst);
        self.data.downcast_ref::<T>().cloned()
    }

    /// Refreshes the cached_at timestamp to the current time.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `CacheEntry`.
    ///
    /// # Side Effects
    ///
    /// Updates the `cached_at` timestamp to the current time.
    pub fn refresh_timestamp(&self) {
        if let Ok(mut cached_at) = self.cached_at.lock() {
            *cached_at = Instant::now();
        }
    }

    /// Checks if the cache entry has expired based on the given expiration duration.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `CacheEntry`.
    /// * `expiration` - The duration after which the entry is considered expired.
    ///
    /// # Returns
    ///
    /// A boolean indicating whether the entry has expired.
    pub fn is_expired(&self, expiration: Duration) -> bool {
        if let Ok(cached_at) = self.cached_at.lock() {
            cached_at.elapsed() > expiration
        } else {
            false
        }
    }

    /// Checks if the cache entry is stale based on the given stale time.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `CacheEntry`.
    /// * `stale_time` - The duration after which the entry is considered stale.
    ///
    /// # Returns
    ///
    /// A boolean indicating whether the entry is stale.
    pub fn is_stale(&self, stale_time: Duration) -> bool {
        if let Ok(cached_at) = self.cached_at.lock() {
            cached_at.elapsed() > stale_time
        } else {
            false
        }
    }

    /// Gets the current access count for the cache entry.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `CacheEntry`.
    ///
    /// # Returns
    ///
    /// The current access count as a `u32`.
    pub fn access_count(&self) -> u32 {
        self.access_count.load(Ordering::SeqCst)
    }

    /// Checks if the cache entry hasn't been accessed for the given duration.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `CacheEntry`.
    /// * `duration` - The duration after which the entry is considered unused.
    ///
    /// # Returns
    ///
    /// A boolean indicating whether the entry is unused.
    pub fn is_unused_for(&self, duration: Duration) -> bool {
        if let Ok(last_accessed) = self.last_accessed.lock() {
            last_accessed.elapsed() > duration
        } else {
            false
        }
    }

    /// Gets the time since this entry was last accessed.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `CacheEntry`.
    ///
    /// # Returns
    ///
    /// A `Duration` representing the time since last access.
    pub fn time_since_last_access(&self) -> Duration {
        if let Ok(last_accessed) = self.last_accessed.lock() {
            last_accessed.elapsed()
        } else {
            Duration::from_secs(0)
        }
    }

    /// Gets the age of this cache entry.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `CacheEntry`.
    ///
    /// # Returns
    ///
    /// A `Duration` representing the age of the entry.
    pub fn age(&self) -> Duration {
        if let Ok(cached_at) = self.cached_at.lock() {
            cached_at.elapsed()
        } else {
            Duration::from_secs(0)
        }
    }
}

/// Global cache for provider results with automatic cleanup
#[derive(Clone, Default)]
pub struct ProviderCache {
    pub cache: Arc<Mutex<HashMap<String, CacheEntry>>>,
}

impl ProviderCache {
    /// Creates a new provider cache.
    ///
    /// # Returns
    ///
    /// A new `ProviderCache` instance.
    pub fn new() -> Self {
        Self::default()
    }

    /// Retrieves a cached result by key.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    /// * `key` - The key to retrieve.
    ///
    /// # Returns
    ///
    /// An `Option<T>` containing the cached data if available, or `None` if not found.
    ///
    /// # Side Effects
    ///
    /// None.
    pub fn get<T: Clone + Send + Sync + 'static>(&self, key: &str) -> Option<T> {
        self.cache.lock().ok()?.get(key)?.get::<T>()
    }

    /// Retrieves a cached result with configurable options
    ///
    /// This unified method handles expiration, staleness checking, and other cache retrieval options.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    /// * `key` - The key to retrieve.
    /// * `options` - Cache retrieval options (expiration, stale time, etc.)
    ///
    /// # Returns
    ///
    /// An `Option<CacheGetResult<T>>` containing the cached data and staleness info if available.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use dioxus_provider::cache::{ProviderCache, CacheGetOptions};
    /// use std::time::Duration;
    ///
    /// let cache = ProviderCache::new();
    /// let options = CacheGetOptions::new()
    ///     .with_expiration(Duration::from_secs(300))
    ///     .with_stale_time(Duration::from_secs(60));
    ///
    /// if let Some(result) = cache.get_with_options::<String>("my_key", options) {
    ///     println!("Data: {}, Stale: {}", result.data, result.is_stale);
    /// }
    /// ```
    pub fn get_with_options<T: Clone + Send + Sync + 'static>(
        &self,
        key: &str,
        options: CacheGetOptions,
    ) -> Option<CacheGetResult<T>> {
        let cache_guard = self.cache.lock().ok()?;
        let entry = cache_guard.get(key)?;

        // Check expiration first
        if let Some(exp_duration) = options.expiration {
            if entry.is_expired(exp_duration) {
                drop(cache_guard);
                // Remove expired entry
                if let Ok(mut cache) = self.cache.lock() {
                    cache.remove(key);
                    crate::debug_log!(
                        "🗑️ [CACHE-EXPIRATION] Removing expired cache entry for key: {}",
                        key
                    );
                }
                return None;
            }
        }

        // Get the data
        let data = entry.get::<T>()?;

        // Check staleness if requested
        let is_stale = if options.check_staleness {
            if let Some(stale_duration) = options.stale_time {
                entry.is_stale(stale_duration)
            } else {
                false
            }
        } else {
            false
        };

        Some(CacheGetResult { data, is_stale })
    }

    /// Retrieves a cached result by key, checking for expiration with a specific expiration duration.
    ///
    /// # Deprecated
    /// Use `get_with_options()` instead for more flexible cache retrieval.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    /// * `key` - The key to retrieve.
    /// * `expiration` - An optional duration after which the entry is considered expired.
    ///
    /// # Returns
    ///
    /// An `Option<T>` containing the cached data if available and not expired, or `None` if expired.
    ///
    /// # Side Effects
    ///
    /// If expired, the entry is removed from the cache.
    #[deprecated(
        since = "0.1.0",
        note = "Use get_with_options() instead for more flexible cache retrieval"
    )]
    pub fn get_with_expiration<T: Clone + Send + Sync + 'static>(
        &self,
        key: &str,
        expiration: Option<Duration>,
    ) -> Option<T> {
        // First, check if the entry exists and is expired
        let is_expired = {
            let cache_guard = self.cache.lock().ok()?;
            let entry = cache_guard.get(key)?;

            if let Some(exp_duration) = expiration {
                entry.is_expired(exp_duration)
            } else {
                false
            }
        };

        // If expired, remove the entry
        if is_expired {
            if let Ok(mut cache) = self.cache.lock() {
                cache.remove(key);
                crate::debug_log!(
                    "🗑️ [CACHE-EXPIRATION] Removing expired cache entry for key: {}",
                    key
                );
            }
            return None;
        }

        // Entry is not expired, return the data
        let cache_guard = self.cache.lock().ok()?;
        let entry = cache_guard.get(key)?;
        entry.get::<T>()
    }

    /// Retrieves cached data with staleness information for SWR behavior.
    ///
    /// # Deprecated
    /// Use `get_with_options()` instead for more flexible cache retrieval.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    /// * `key` - The key to retrieve.
    /// * `stale_time` - An optional duration after which the entry is considered stale.
    /// * `expiration` - An optional duration after which the entry is considered expired.
    ///
    /// # Returns
    ///
    /// An `Option<(T, bool)>` containing the cached data and a boolean indicating staleness.
    ///
    /// # Side Effects
    ///
    /// None.
    #[deprecated(
        since = "0.1.0",
        note = "Use get_with_options() instead for more flexible cache retrieval"
    )]
    pub fn get_with_staleness<T: Clone + Send + Sync + 'static>(
        &self,
        key: &str,
        stale_time: Option<Duration>,
        expiration: Option<Duration>,
    ) -> Option<(T, bool)> {
        let cache_guard = self.cache.lock().ok()?;
        let entry = cache_guard.get(key)?;

        // Check if expired first
        if let Some(exp_duration) = expiration
            && entry.is_expired(exp_duration)
        {
            return None;
        }

        // Get the data
        let data = entry.get::<T>()?;

        // Check if stale
        let is_stale = if let Some(stale_duration) = stale_time {
            entry.is_stale(stale_duration)
        } else {
            false
        };

        Some((data, is_stale))
    }

    /// Sets a value for a given key.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    /// * `key` - The key to set.
    /// * `value` - The value to set.
    ///
    /// # Returns
    ///
    /// A boolean indicating whether the value was updated (true) or unchanged (false).
    ///
    /// # Side Effects
    ///
    /// Updates the `cached_at` timestamp if the value was updated.
    pub fn set<T: Clone + Send + Sync + PartialEq + 'static>(&self, key: String, value: T) -> bool {
        if let Ok(mut cache) = self.cache.lock() {
            if let Some(existing_entry) = cache.get_mut(&key)
                && let Some(existing_value) = existing_entry.get::<T>()
                && existing_value == value
            {
                existing_entry.refresh_timestamp();
                crate::debug_log!(
                    "⏸️ [CACHE-STORE] Value unchanged for key: {}, refreshing timestamp",
                    key
                );
                return false;
            }
            cache.insert(key.clone(), CacheEntry::new(value));
            crate::debug_log!("📊 [CACHE-STORE] Stored data for key: {}", key);
            return true;
        }
        false
    }

    /// Removes a cached result by key.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    /// * `key` - The key to remove.
    ///
    /// # Returns
    ///
    /// A boolean indicating whether the entry was removed.
    ///
    /// # Side Effects
    ///
    /// None.
    pub fn remove(&self, key: &str) -> bool {
        if let Ok(mut cache) = self.cache.lock() {
            cache.remove(key).is_some()
        } else {
            false
        }
    }

    /// Invalidates a cached result by key (alias for remove).
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    /// * `key` - The key to invalidate.
    ///
    /// # Side Effects
    ///
    /// The entry is removed from the cache.
    pub fn invalidate(&self, key: &str) {
        self.remove(key);
        crate::debug_log!(
            "🗑️ [CACHE-INVALIDATE] Invalidated cache entry for key: {}",
            key
        );
    }

    /// Clears all cached results.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    ///
    /// # Side Effects
    ///
    /// All entries are removed from the cache.
    pub fn clear(&self) {
        if let Ok(mut cache) = self.cache.lock() {
            #[cfg(feature = "tracing")]
            let count = cache.len();
            cache.clear();
            #[cfg(feature = "tracing")]
            crate::debug_log!("🗑️ [CACHE-CLEAR] Cleared {} cache entries", count);
        }
    }

    /// Gets the number of cached entries.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    ///
    /// # Returns
    ///
    /// The number of cached entries as a `usize`.
    ///
    /// # Side Effects
    ///
    /// None.
    pub fn size(&self) -> usize {
        self.cache.lock().map(|cache| cache.len()).unwrap_or(0)
    }

    /// Cleans up unused entries based on access time.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    /// * `unused_threshold` - The duration after which an entry is considered unused.
    ///
    /// # Returns
    ///
    /// The number of unused entries removed.
    ///
    /// # Side Effects
    ///
    /// Unused entries are removed from the cache.
    pub fn cleanup_unused_entries(&self, unused_threshold: Duration) -> usize {
        if let Ok(mut cache) = self.cache.lock() {
            let initial_size = cache.len();
            cache.retain(|_key, entry| {
                let should_keep = !entry.is_unused_for(unused_threshold);
                #[cfg(feature = "tracing")]
                if !should_keep {
                    crate::debug_log!("🧹 [CACHE-CLEANUP] Removing unused entry: {}", _key);
                }
                should_keep
            });
            let removed = initial_size - cache.len();
            if removed > 0 {
                crate::debug_log!("🧹 [CACHE-CLEANUP] Removed {} unused entries", removed);
            }
            removed
        } else {
            0
        }
    }

    /// Evicts least recently used entries to maintain cache size limit.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    /// * `max_size` - The maximum number of entries to keep.
    ///
    /// # Returns
    ///
    /// The number of entries evicted.
    ///
    /// # Side Effects
    ///
    /// Least recently used entries are removed from the cache.
    pub fn evict_lru_entries(&self, max_size: usize) -> usize {
        if let Ok(mut cache) = self.cache.lock() {
            if cache.len() <= max_size {
                return 0;
            }

            // Convert to vector for sorting
            let mut entries: Vec<_> = cache.drain().collect();

            // Sort by last access time (oldest first)
            entries.sort_by(|(_, a), (_, b)| {
                a.time_since_last_access().cmp(&b.time_since_last_access())
            });

            // Keep the most recently used entries
            let to_keep = entries.split_off(entries.len().saturating_sub(max_size));
            let evicted = entries.len();

            // Rebuild cache with kept entries
            cache.extend(to_keep);

            if evicted > 0 {
                crate::debug_log!(
                    "🗑️ [LRU-EVICT] Evicted {} entries due to cache size limit",
                    evicted
                );
            }
            evicted
        } else {
            0
        }
    }

    /// Performs comprehensive cache maintenance.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    ///
    /// # Returns
    ///
    /// A `CacheMaintenanceStats` containing statistics about the maintenance.
    ///
    /// # Side Effects
    ///
    /// Unused entries are removed and LRU entries are evicted.
    pub fn maintain(&self) -> CacheMaintenanceStats {
        CacheMaintenanceStats {
            unused_removed: self.cleanup_unused_entries(DEFAULT_UNUSED_THRESHOLD),
            lru_evicted: self.evict_lru_entries(DEFAULT_MAX_CACHE_SIZE),
            final_size: self.size(),
        }
    }

    /// Gets cache statistics.
    ///
    /// # Arguments
    ///
    /// * `&self` - A reference to the `ProviderCache`.
    ///
    /// # Returns
    ///
    /// A `CacheStats` containing statistics about the cache.
    ///
    /// # Side Effects
    ///
    /// None.
    pub fn stats(&self) -> CacheStats {
        if let Ok(cache) = self.cache.lock() {
            let mut total_age = Duration::ZERO;
            let mut total_accesses = 0;

            for entry in cache.values() {
                total_age += entry.age();
                total_accesses += entry.access_count();
            }

            let entry_count = cache.len();
            let avg_age = if entry_count > 0 {
                total_age / entry_count as u32
            } else {
                Duration::ZERO
            };

            CacheStats {
                entry_count,
                total_accesses,
                total_references: 0, // No longer tracking references
                avg_age,
                total_size_bytes: entry_count * 1024, // Rough estimate
            }
        } else {
            CacheStats::default()
        }
    }
}

/// Statistics for cache maintenance operations
#[derive(Debug, Clone, Default)]
pub struct CacheMaintenanceStats {
    pub unused_removed: usize,
    pub lru_evicted: usize,
    pub final_size: usize,
}

/// General cache statistics
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    pub entry_count: usize,
    pub total_accesses: u32,
    pub total_references: u32,
    pub avg_age: Duration,
    pub total_size_bytes: usize,
}

impl CacheStats {
    pub fn avg_accesses_per_entry(&self) -> f64 {
        if self.entry_count > 0 {
            self.total_accesses as f64 / self.entry_count as f64
        } else {
            0.0
        }
    }

    pub fn avg_references_per_entry(&self) -> f64 {
        if self.entry_count > 0 {
            self.total_references as f64 / self.entry_count as f64
        } else {
            0.0
        }
    }
}