Struct http_cache::MokaCacheBuilder
source · [−]pub struct MokaCacheBuilder<K, V, C> { /* private fields */ }manager-moka only.Expand description
Builds a Cache with various configuration knobs.
Examples
// Cargo.toml
//
// [dependencies]
// moka = { version = "0.8", features = ["future"] }
// tokio = { version = "1", features = ["rt-multi-thread", "macros" ] }
// futures = "0.3"
use moka::future::Cache;
use std::time::Duration;
#[tokio::main]
async fn main() {
let cache = Cache::builder()
// Max 10,000 entries
.max_capacity(10_000)
// Time to live (TTL): 30 minutes
.time_to_live(Duration::from_secs(30 * 60))
// Time to idle (TTI): 5 minutes
.time_to_idle(Duration::from_secs( 5 * 60))
// Create the cache.
.build();
// This entry will expire after 5 minutes (TTI) if there is no get().
cache.insert(0, "zero").await;
// This get() will extend the entry life for another 5 minutes.
cache.get(&0);
// Even though we keep calling get(), the entry will expire
// after 30 minutes (TTL) from the insert().
}Implementations
sourceimpl<K, V> CacheBuilder<K, V, Cache<K, V, RandomState>> where
K: 'static + Eq + Hash + Send + Sync,
V: 'static + Clone + Send + Sync,
impl<K, V> CacheBuilder<K, V, Cache<K, V, RandomState>> where
K: 'static + Eq + Hash + Send + Sync,
V: 'static + Clone + Send + Sync,
sourcepub fn new(max_capacity: u64) -> CacheBuilder<K, V, Cache<K, V, RandomState>>
pub fn new(max_capacity: u64) -> CacheBuilder<K, V, Cache<K, V, RandomState>>
Construct a new CacheBuilder that will be used to build a Cache holding
up to max_capacity entries.
sourcepub fn build(self) -> Cache<K, V, RandomState>
pub fn build(self) -> Cache<K, V, RandomState>
Builds a Cache<K, V>.
Panics
Panics if configured with either time_to_live or time_to_idle higher than
1000 years. This is done to protect against overflow when computing key
expiration.
sourcepub fn build_with_hasher<S>(self, hasher: S) -> Cache<K, V, S> where
S: 'static + BuildHasher + Clone + Send + Sync,
pub fn build_with_hasher<S>(self, hasher: S) -> Cache<K, V, S> where
S: 'static + BuildHasher + Clone + Send + Sync,
Builds a Cache<K, V, S>, with the given hasher.
Panics
Panics if configured with either time_to_live or time_to_idle higher than
1000 years. This is done to protect against overflow when computing key
expiration.
sourceimpl<K, V, C> CacheBuilder<K, V, C>
impl<K, V, C> CacheBuilder<K, V, C>
sourcepub fn max_capacity(self, max_capacity: u64) -> CacheBuilder<K, V, C>
pub fn max_capacity(self, max_capacity: u64) -> CacheBuilder<K, V, C>
Sets the max capacity of the cache.
sourcepub fn initial_capacity(self, number_of_entries: usize) -> CacheBuilder<K, V, C>
pub fn initial_capacity(self, number_of_entries: usize) -> CacheBuilder<K, V, C>
Sets the initial capacity (number of entries) of the cache.
sourcepub fn weigher(
self,
weigher: impl Fn(&K, &V) -> u32 + Send + Sync + 'static
) -> CacheBuilder<K, V, C>
pub fn weigher(
self,
weigher: impl Fn(&K, &V) -> u32 + Send + Sync + 'static
) -> CacheBuilder<K, V, C>
Sets the weigher closure of the cache.
The closure should take &K and &V as the arguments and returns a u32
representing the relative size of the entry.
sourcepub fn time_to_live(self, duration: Duration) -> CacheBuilder<K, V, C>
pub fn time_to_live(self, duration: Duration) -> CacheBuilder<K, V, C>
Sets the time to live of the cache.
A cached entry will be expired after the specified duration past from
insert.
Panics
CacheBuilder::build* methods will panic if the given duration is longer
than 1000 years. This is done to protect against overflow when computing key
expiration.
sourcepub fn time_to_idle(self, duration: Duration) -> CacheBuilder<K, V, C>
pub fn time_to_idle(self, duration: Duration) -> CacheBuilder<K, V, C>
Sets the time to idle of the cache.
A cached entry will be expired after the specified duration past from get
or insert.
Panics
CacheBuilder::build* methods will panic if the given duration is longer
than 1000 years. This is done to protect against overflow when computing key
expiration.
sourcepub fn support_invalidation_closures(self) -> CacheBuilder<K, V, C>
pub fn support_invalidation_closures(self) -> CacheBuilder<K, V, C>
Enables support for Cache::invalidate_entries_if method.
The cache will maintain additional internal data structures to support
invalidate_entries_if method.
Trait Implementations
sourceimpl<K, V> Default for CacheBuilder<K, V, Cache<K, V, RandomState>> where
K: 'static + Eq + Hash + Send + Sync,
V: 'static + Clone + Send + Sync,
impl<K, V> Default for CacheBuilder<K, V, Cache<K, V, RandomState>> where
K: 'static + Eq + Hash + Send + Sync,
V: 'static + Clone + Send + Sync,
sourcefn default() -> CacheBuilder<K, V, Cache<K, V, RandomState>>
fn default() -> CacheBuilder<K, V, Cache<K, V, RandomState>>
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl<K, V, C> !RefUnwindSafe for CacheBuilder<K, V, C>
impl<K, V, C> Send for CacheBuilder<K, V, C> where
C: Send,
impl<K, V, C> Sync for CacheBuilder<K, V, C> where
C: Sync,
impl<K, V, C> Unpin for CacheBuilder<K, V, C> where
C: Unpin,
impl<K, V, C> !UnwindSafe for CacheBuilder<K, V, C>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more