pub struct RestCache<K, V>{ /* private fields */ }Expand description
Concurrent TTL cache.
Use RestCache::get_or_fetch to look up a value, fetching from the loader if
missing or expired. The loader is async and returns Result<V, E>.
Clone is cheap — the internal map is Arc-wrapped.
Implementations§
Source§impl<K, V> RestCache<K, V>
impl<K, V> RestCache<K, V>
Sourcepub fn new(default_ttl: Duration) -> Self
pub fn new(default_ttl: Duration) -> Self
Create a new cache with the given default TTL applied by RestCache::insert.
Sourcepub fn insert_with_ttl(&self, key: K, value: V, ttl: Duration)
pub fn insert_with_ttl(&self, key: K, value: V, ttl: Duration)
Insert with an explicit TTL.
Sourcepub async fn get_or_fetch<F, Fut, E>(&self, key: K, loader: F) -> Result<V, E>
pub async fn get_or_fetch<F, Fut, E>(&self, key: K, loader: F) -> Result<V, E>
Get or fetch.
Returns the cached value if fresh. Otherwise calls loader, caches the result
with the default TTL, and returns it.
See the module-level doc for single-flight semantics (short: not single-flight).
Sourcepub fn invalidate(&self, key: &K)
pub fn invalidate(&self, key: &K)
Manually expire one key (next get or get_or_fetch will reload).
Sourcepub fn sweep_expired(&self) -> usize
pub fn sweep_expired(&self) -> usize
Eviction sweep — removes all expired entries.
Returns the number of entries removed. Call periodically if the cache can accumulate many distinct keys (e.g. per-symbol caches).