do_memory_storage_redb/cache/traits.rs
1//! Cache trait for abstracting cache implementations
2//!
3//! This module defines the common interface for cache implementations,
4//! allowing different caching strategies (LRU, Adaptive, etc.) to be
5//! used interchangeably.
6
7use async_trait::async_trait;
8use uuid::Uuid;
9
10use super::types::CacheMetrics;
11
12/// Common cache trait for metadata tracking
13///
14/// This trait abstracts the cache interface, allowing different implementations
15/// (LRU, Adaptive TTL) to be used interchangeably in `RedbStorage`.
16///
17/// All implementations must be `Send + Sync` for thread-safe async access.
18#[async_trait]
19pub trait Cache: Send + Sync {
20 /// Record a cache access (hit or miss)
21 ///
22 /// # Arguments
23 ///
24 /// * `id` - Unique identifier for the cache entry
25 /// * `hit` - Whether this was a cache hit (true) or miss (false)
26 /// * `size_bytes` - Optional size in bytes (for size-aware eviction)
27 ///
28 /// # Returns
29 ///
30 /// `true` if the entry was found and is valid, `false` otherwise
31 async fn record_access(&self, id: Uuid, hit: bool, size_bytes: Option<usize>) -> bool;
32
33 /// Remove an entry from the cache
34 async fn remove(&self, id: Uuid);
35
36 /// Check if an entry exists and is not expired
37 async fn contains(&self, id: Uuid) -> bool;
38
39 /// Get current cache metrics
40 async fn get_metrics(&self) -> CacheMetrics;
41
42 /// Clear all entries from cache
43 async fn clear(&self);
44
45 /// Manually cleanup expired entries
46 ///
47 /// Returns the number of expired entries removed.
48 async fn cleanup_expired(&self) -> usize;
49}