Skip to main content

micromegas_object_cache/
backend.rs

1use async_trait::async_trait;
2use bytes::Bytes;
3
4/// Hints the backend's fill priority: prefetch fills should not evict hot
5/// demand data from a bounded cache tier.
6#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7pub enum FillHint {
8    Demand,
9    Prefetch,
10}
11
12/// Point-in-time disk write-path counters (cumulative since process start).
13/// foyer-independent so the trait stays buildable without the `foyer`
14/// feature; `None` for backends with no disk tier (e.g. in-memory).
15#[derive(Clone, Copy, Debug, Default)]
16pub struct BackendDiskStats {
17    pub write_bytes: u64,
18    pub read_bytes: u64,
19    pub write_ios: u64,
20    pub read_ios: u64,
21}
22
23#[async_trait]
24pub trait RangeCacheBackend: Send + Sync {
25    /// `expected_len` is the exact length the caller will accept for `key`.
26    /// A backend MUST NOT copy a value whose length differs into any faster
27    /// tier it maintains (the promotion gate) and MUST treat such a value as
28    /// a miss. Single-tier backends accept and ignore the parameter.
29    async fn get(&self, key: &str, expected_len: u64) -> Option<Bytes>;
30    async fn put(&self, key: String, value: Bytes, hint: FillHint);
31
32    /// Disk write-path counters, for the saturation monitor's per-second
33    /// gauges. Defaulted to `None` so backends without a disk tier (e.g.
34    /// `MemoryBackend`) need no override.
35    fn disk_stats(&self) -> Option<BackendDiskStats> {
36        None
37    }
38
39    /// Accounted RAM-tier byte usage, for the saturation monitor's residency
40    /// gauge. `None` for backends with no RAM tier accounting (e.g.
41    /// `MemoryBackend`). Divergence between this and process RSS is the
42    /// signature of a cached value pinning more than its accounted weight.
43    fn ram_usage_bytes(&self) -> Option<usize> {
44        None
45    }
46
47    /// Accounted RAM-tier entry count, the entry-count sibling to
48    /// `ram_usage_bytes`. `None` for backends with no RAM tier accounting
49    /// (e.g. `MemoryBackend`).
50    fn ram_entries(&self) -> Option<usize> {
51        None
52    }
53}