pub struct AuthCache { /* private fields */ }Expand description
Authentication cache with configurable TTL.
Beyond basic positive caching, this provides:
- Traffic deltas (
user_id → i64): accumulated traffic bytes since the last DB fetch, applied on cache hit so traffic-limit checks are accurate within a single cache window. - Negative cache (
hash → expiry): short-lived entries for hashes that returned no DB row, preventing repeated SELECT storms from invalid/attack traffic.
Implementations§
Source§impl AuthCache
impl AuthCache
Sourcepub fn new(ttl: Duration, neg_ttl: Duration) -> Self
pub fn new(ttl: Duration, neg_ttl: Duration) -> Self
Create a new auth cache.
ttl— positive cache entry lifetimeneg_ttl— negative cache entry lifetime (Duration::ZEROto disable)
Sourcepub fn get(&self, hash: &str) -> Option<CachedUser>
pub fn get(&self, hash: &str) -> Option<CachedUser>
Get a cached user by password hash.
Returns Some(CachedUser) if found and not expired, None otherwise.
Sourcepub fn insert(&self, hash: String, user: CachedUser)
pub fn insert(&self, hash: String, user: CachedUser)
Insert a user into the cache.
Sourcepub fn invalidate_user(&self, user_id: &str)
pub fn invalidate_user(&self, user_id: &str)
Invalidate a user by user_id.
Removes all positive cache entries with matching user_id and clears the traffic delta for that user.
Sourcepub fn cleanup_expired(&self)
pub fn cleanup_expired(&self)
Remove expired entries from positive and negative caches.
Sourcepub fn add_traffic_delta(&self, user_id: &str, bytes: u64)
pub fn add_traffic_delta(&self, user_id: &str, bytes: u64)
Increment the in-memory traffic delta for a user.
Called by StoreAuth::record_traffic() so that subsequent
cache hits reflect the accumulated traffic.
Sourcepub fn get_traffic_delta(&self, user_id: &str) -> i64
pub fn get_traffic_delta(&self, user_id: &str) -> i64
Read the accumulated traffic delta for a user.
Returns 0 if no delta is tracked (user never had traffic recorded since the last DB fetch).
Sourcepub fn clear_traffic_delta(&self, user_id: &str)
pub fn clear_traffic_delta(&self, user_id: &str)
Clear the traffic delta for a user.
Called when the cache re-fetches from DB, so the delta restarts from zero (the DB value is now authoritative).
Sourcepub fn insert_negative(&self, hash: &str)
pub fn insert_negative(&self, hash: &str)
Record a hash as “not found” in the negative cache.
Subsequent lookups within neg_ttl will return true from
is_negative, skipping the DB query.
Sourcepub fn is_negative(&self, hash: &str) -> bool
pub fn is_negative(&self, hash: &str) -> bool
Check if a hash is in the negative cache (known invalid).
Returns true if the hash was recently looked up and not found.
Expired entries are lazily removed.
Sourcepub fn remove_negative(&self, hash: &str)
pub fn remove_negative(&self, hash: &str)
Remove a hash from the negative cache.
Called after cache invalidation so that a newly-added user is not blocked by a stale negative entry.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Get cache statistics.