Skip to main content

hyperlane_plugin_cache/
struct.rs

1use super::*;
2
3/// Cached HTTP response or arbitrary byte payload with headers and timestamp metadata.
4#[derive(Clone, Data, Debug, Eq, PartialEq)]
5pub struct CachedValue {
6    /// Cached HTTP status code (for example `200`).
7    #[get(type(copy))]
8    pub(crate) status_code: usize,
9    /// Cached response headers `(name, value)`.
10    pub(crate) headers: Vec<(String, String)>,
11    /// Cached payload bytes.
12    pub(crate) body: Vec<u8>,
13    /// Unix epoch millisecond when this entry was stored.
14    #[get(type(copy))]
15    pub(crate) stored_at_ms: u64,
16}
17
18/// Internal cache record pairing a [`CachedValue`] with expiration and byte footprint.
19pub(crate) struct CacheEntry {
20    /// Cached payload.
21    pub(crate) value: CachedValue,
22    /// Unix epoch millisecond at which this entry expires.
23    pub(crate) expires_at_ms: u64,
24    /// Approximate memory footprint in bytes for budget enforcement.
25    pub(crate) footprint_bytes: usize,
26}
27
28/// Single-flight coordination slot coalescing concurrent cache misses for one key.
29pub(crate) struct InFlightSlot {
30    /// Notifier woken once the leader finishes loading.
31    pub(crate) notify: Notify,
32    /// Completed result shared with all waiting followers.
33    pub(crate) result: Mutex<Option<Option<CachedValue>>>,
34}
35
36/// Configuration for [`ShardedMemoryCache`] and [`CachePlugin`].
37#[derive(Clone, Data, Debug, Eq, PartialEq)]
38pub struct CacheConfig {
39    /// Default entry TTL in milliseconds when no `Cache-Control: max-age` override is present.
40    #[get(type(copy))]
41    pub(crate) default_ttl_ms: u64,
42    /// Maximum cached entries across all 64 shards.
43    #[get(type(copy))]
44    pub(crate) max_entries: usize,
45    /// Maximum aggregate memory footprint across all 64 shards in bytes.
46    #[get(type(copy))]
47    pub(crate) max_bytes: usize,
48    /// Maximum single response body length eligible for automatic HTTP response caching.
49    #[get(type(copy))]
50    pub(crate) max_entry_bytes: usize,
51    /// Bypasses HTTP response caching when `Authorization` or `Cookie` is present when `true`.
52    #[get(type(copy))]
53    pub(crate) bypass_authenticated: bool,
54    /// Emits diagnostic `X-Cache` and `Age` headers when `true`.
55    #[get(type(copy))]
56    pub(crate) emit_diagnostic_headers: bool,
57}
58
59/// Shared interior state of [`ShardedMemoryCache`].
60pub(crate) struct ShardedMemoryCacheInner {
61    /// Cache capacity and TTL configuration.
62    pub(crate) config: CacheConfig,
63    /// Lock-free count of live entries across all shards.
64    pub(crate) entry_count: AtomicUsize,
65    /// Lock-free estimated total byte usage across all shards.
66    pub(crate) total_bytes: AtomicUsize,
67    /// 64-way sharded entry tables.
68    pub(crate) shards: CacheShardArray,
69    /// 64-way sharded single-flight stampede-coalescing map.
70    pub(crate) inflight: InFlightShardArray,
71}
72
73/// High-concurrency 64-way sharded bounded in-memory [`CacheStore`] with single-flight coalescing.
74#[derive(Clone)]
75pub struct ShardedMemoryCache {
76    /// Shared sharded cache state.
77    pub(crate) inner: Arc<ShardedMemoryCacheInner>,
78}
79
80/// Process-wide [`CachePlugin`] singleton storing [`CacheConfig`] and a pluggable [`CacheStore`].
81#[derive(Clone)]
82pub struct CachePlugin {
83    /// Active HTTP caching policy.
84    pub(crate) config: CacheConfig,
85    /// Backing cache store (default [`ShardedMemoryCache`] or custom distributed store).
86    pub(crate) store: SharedCacheStore,
87}
88
89/// Opt-in request middleware serving cached `GET`/`HEAD` responses with `X-Cache: HIT` and `Age`.
90#[derive(Clone, Copy, Debug, Default)]
91#[hyperlane::request_middleware(-80)]
92pub struct CacheRequestHook;
93
94/// Opt-in response middleware storing eligible `200 OK` responses according to `Cache-Control` and `Vary`.
95#[derive(Clone, Copy, Debug, Default)]
96#[hyperlane::response_middleware(-65)]
97pub struct CacheResponseHook;