pub struct AuthCache {
inner: Arc<RwLock<LruCache<String, AuthEntry>>>,
locks: Arc<DashMap<String, Arc<Mutex<()>>>>,
}Expand description
A time-bounded, capacity-limited LRU cache for authorization decisions.
Wraps arbitrary async validator functions so that the (potentially
expensive) Python callback is only invoked once per (access_key, bucket, method) tuple within the configured TTL window.
Memory is bounded by capacity: when the limit is reached the
least-recently-used entry is evicted automatically (TODO perf-4: done).
Concurrent cache misses for the same key are serialised via a per-key
[Mutex] to avoid thundering-herd stampedes. Misses for different keys
are fully concurrent — the per-key lock map is backed by a [DashMap] so
no single global lock is held (TODO perf-3: done).
Fields§
§inner: Arc<RwLock<LruCache<String, AuthEntry>>>§locks: Arc<DashMap<String, Arc<Mutex<()>>>>Per-key mutex map — DashMap so concurrent misses for different keys never contend on a shared lock.
Implementations§
Source§impl AuthCache
impl AuthCache
pub fn new(capacity: usize) -> Self
pub async fn get_or_validate<F, Fut, E>( &self, key: &str, ttl: Duration, validator_fn: F, ) -> Result<bool, E>
Sourcepub fn insert(&self, key: String, authorized: bool, ttl: Duration)
pub fn insert(&self, key: String, authorized: bool, ttl: Duration)
Pre-populate the cache with a known decision for key.
Sourcepub fn invalidate(&self, key: &str)
pub fn invalidate(&self, key: &str)
Evict the cached entry for key, forcing re-validation on the next request.