Skip to main content

AuthCache

Struct AuthCache 

Source
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

Source

pub fn new(ttl: Duration, neg_ttl: Duration) -> Self

Create a new auth cache.

  • ttl — positive cache entry lifetime
  • neg_ttl — negative cache entry lifetime (Duration::ZERO to disable)
Source

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.

Source

pub fn insert(&self, hash: String, user: CachedUser)

Insert a user into the cache.

Source

pub fn remove(&self, hash: &str)

Remove a user from the cache.

Source

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.

Source

pub fn clear(&self)

Clear all cache entries (positive, negative, and traffic deltas).

Source

pub fn cleanup_expired(&self)

Remove expired entries from positive and negative caches.

Source

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.

Source

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).

Source

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).

Source

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.

Source

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.

Source

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.

Source

pub fn stats(&self) -> CacheStats

Get cache statistics.

Trait Implementations§

Source§

impl Debug for AuthCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.