pub struct TokenSourceCached<S> { /* private fields */ }Expand description
The return type of TokenSourceConfigurable::cached: wraps another token
source and stores the last fetched credentials, serving them for repeat
fetches with equal options for as long as they stay valid.
Credentials are kept in a TokenSourceStore (by default in memory, see
TokenSourceCached::with_store) and are considered valid as long as a
validator accepts them (by default until the token expires, see
TokenSourceCached::with_validator).
Concurrent fetches that miss the cache are not deduplicated; each hits the underlying source and the last response wins.
Implementations§
Source§impl<S> TokenSourceCached<S>
impl<S> TokenSourceCached<S>
Sourcepub fn with_store(self, store: impl TokenSourceStore + 'static) -> Self
pub fn with_store(self, store: impl TokenSourceStore + 'static) -> Self
Replaces the default in-memory store with a custom TokenSourceStore,
e.g. one persisting credentials to the platform keychain or a database.
Sourcepub fn with_validator<F>(self, validator: F) -> Self
pub fn with_validator<F>(self, validator: F) -> Self
Replaces the default validator deciding whether stored credentials are
still valid or must be refetched. The default checks that the token has
not expired (TokenSourceResponse::has_valid_token).
Pass the closure inline (or annotate its parameter types); binding it to a variable first can fail closure type inference.
Sourcepub async fn invalidate(&self)
pub async fn invalidate(&self)
Removes the stored credentials, forcing the next fetch to hit the underlying source.
A fetch already in flight is unaffected: it still resolves and stores its response afterwards, repopulating the cache (last writer wins).
Sourcepub async fn cached_response(&self) -> Option<TokenSourceResponse>
pub async fn cached_response(&self) -> Option<TokenSourceResponse>
Returns the last stored response, if any — without checking its validity or which options it was fetched with, so it may be expired.
Trait Implementations§
Source§impl<S: TokenSourceConfigurable + Send + Sync> TokenSourceConfigurable for TokenSourceCached<S>
impl<S: TokenSourceConfigurable + Send + Sync> TokenSourceConfigurable for TokenSourceCached<S>
fn fetch<'life0, 'life1, 'async_trait>(
&'life0 self,
options: &'life1 TokenSourceFetchOptions,
) -> Pin<Box<dyn Future<Output = TokenSourceResult<TokenSourceResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn cached(self) -> TokenSourceCached<Self>
fn cached(self) -> TokenSourceCached<Self>
TokenSourceCached.