pub struct TokenCache { /* private fields */ }Expand description
Thread-safe cache for storing authentication tokens with expiration tracking.
Provides a simple key-value store for authentication tokens that automatically handles expiration checking and cleanup. The cache is designed to be shared across multiple threads and async tasks safely.
§Thread Safety
All operations are thread-safe and can be called concurrently from multiple async tasks. The cache uses RwLock for efficient concurrent read access.
§Examples
use quetty_server::auth::{TokenCache, CachedToken};
use std::time::{Duration, Instant};
let cache = TokenCache::new();
// Store a token
let token = CachedToken {
token: "access_token_123".to_string(),
expires_at: Instant::now() + Duration::from_secs(3600),
};
cache.set("user_123".to_string(), token).await;
// Retrieve a token (returns None if expired)
if let Some(token) = cache.get("user_123").await {
println!("Token: {}", token);
}Implementations§
Source§impl TokenCache
impl TokenCache
Sourcepub async fn set(&self, key: String, token: CachedToken)
pub async fn set(&self, key: String, token: CachedToken)
Stores a token in the cache.
Overwrites any existing token with the same key.
§Arguments
key- The cache key to store the token undertoken- The cached token with expiration information
Sourcepub async fn invalidate(&self, key: &str)
pub async fn invalidate(&self, key: &str)
Sourcepub async fn clear(&self)
pub async fn clear(&self)
Clears all tokens from the cache.
This is useful for logout operations or when switching authentication contexts.
Sourcepub async fn needs_refresh(&self, key: &str) -> bool
pub async fn needs_refresh(&self, key: &str) -> bool
Trait Implementations§
Source§impl Clone for TokenCache
impl Clone for TokenCache
Source§fn clone(&self) -> TokenCache
fn clone(&self) -> TokenCache
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for TokenCache
impl !RefUnwindSafe for TokenCache
impl Send for TokenCache
impl Sync for TokenCache
impl Unpin for TokenCache
impl !UnwindSafe for TokenCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more