CacheBuilder

Struct CacheBuilder 

Source
pub struct CacheBuilder<K, V, W = ItemCountWeighter, H = FastBuildHasher> { /* private fields */ }
Expand description

Builder for creating a Cache.

Implementations§

Source§

impl<K, V> CacheBuilder<K, V>

Source

pub fn from_identifier<N: Into<String>>( identifier: N, ) -> Result<CacheBuilder<K, V>, GenericError>

Creates a new CacheBuilder with the given cache identifier.

The cache identifier should be unique, but it is not required to be. Metrics for the cache will be emitted using the given identifier, so in cases where the identifier is not unique, those metrics will be aggregated together and it will not be possible to distinguish between the different caches.

§Errors

If the given cache identifier is empty, an error is returned.

Source

pub fn for_tests() -> CacheBuilder<K, V>

Configures a CacheBuilder that is suitable for tests.

This configures the builder with the following defaults:

  • cache identifier of “noop”
  • unlimited cache size
  • telemetry disabled

This is generally only useful for testing purposes, and is exposed publicly in order to be used in cross-crate testing scenarios.

Source§

impl<K, V, W, H> CacheBuilder<K, V, W, H>

Source

pub fn with_capacity(self, capacity: NonZeroUsize) -> Self

Sets the capacity for the cache.

The capacity is used, in conjunction with the item weighter, to determine how many items can be held in the cache and when items should be evicted to make room for new items.

See with_item_weighter for more information on how the item weighter is used.

Defaults to unlimited capacity.

Source

pub fn with_time_to_idle(self, idle_period: Option<Duration>) -> Self

Enables expiration of cached items based on how long since they were last accessed.

Items which have not been accessed within the configured duration will be marked for expiration, and be removed from the cache shortly thereafter. For the purposes of expiration, “accessed” is either when the item was first inserted or when it was last read.

If the given value is None, expiration is disabled.

Defaults to no expiration.

Source

pub fn with_expiration_interval(self, expiration_interval: Duration) -> Self

Sets the interval at which the expiration process will run.

This controls how often the expiration process will run to check for expired items. While items become eligible for expiration after the configured duration, they are not guaranteed to be removed immediately: the expiration process must still run to actually find the expired items and remove them.

This means that the rough upper bound for how long an item may be kept alive is the sum of both the configured expiration duration and the expiration interval.

This value is only relevant if expiration is enabled.

Defaults to 1 second.

Source

pub fn with_item_weighter<W2>(self, weighter: W2) -> CacheBuilder<K, V, W2, H>

Sets the item weighter for the cache.

The item weighter is used to determine the “weight” of each item in the cache, which is used during insertion/eviction to determine if an item can be held in the cache without first having to evict other items to stay within the configured capacity.

For example, if the configured capacity is set to 10,000, and the “item count” weighter is used, then the cache will operate in a way that aims to simply ensure that no more than 10,000 items are held in the cache at any given time. This allows defining custom weighters that can be used to track other aspects of the items in the cache, such as their size in bytes, or some other metric that is relevant to the intended caching behavior.

Defaults to “item count” weighter.

Source

pub fn with_hasher<H2>(self) -> CacheBuilder<K, V, W, H2>

Sets the item hasher for the cache.

As cache keys are hashed before performing any reads or writes, the chosen hasher can potentially impact the performance of those operations. In some scenarios, it may be desirable to use a different hasher than the default one in order to optimize for specific key types or access patterns.

Defaults to a fast, non-cryptographic hasher: FastBuildHasher.

Source

pub fn with_telemetry(self, enabled: bool) -> Self

Sets whether or not to enable telemetry for this cache.

Reporting the telemetry of the cache requires running an asynchronous task to override adding additional overhead in the hot path of reading or writing to the cache. In some cases, it may be cumbersome to always create the cache in an asynchronous context so that the telemetry task can be spawned. This method allows disabling telemetry reporting in those cases.

Defaults to telemetry enabled.

Source§

impl<K, V, W, H> CacheBuilder<K, V, W, H>
where K: Eq + Hash + Clone + Send + Sync + 'static, V: Clone + Send + Sync + 'static, W: Weighter<K, V> + Clone + Send + Sync + 'static, H: BuildHasher + Clone + Default + Send + Sync + 'static,

Source

pub fn build(self) -> Cache<K, V, W, H>

Builds a Cache from the current configuration.

Auto Trait Implementations§

§

impl<K, V, W, H> Freeze for CacheBuilder<K, V, W, H>
where W: Freeze,

§

impl<K, V, W, H> RefUnwindSafe for CacheBuilder<K, V, W, H>

§

impl<K, V, W, H> Send for CacheBuilder<K, V, W, H>
where W: Send, K: Send, V: Send, H: Send,

§

impl<K, V, W, H> Sync for CacheBuilder<K, V, W, H>
where W: Sync, K: Sync, V: Sync, H: Sync,

§

impl<K, V, W, H> Unpin for CacheBuilder<K, V, W, H>
where W: Unpin, K: Unpin, V: Unpin, H: Unpin,

§

impl<K, V, W, H> UnwindSafe for CacheBuilder<K, V, W, H>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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
§

impl<T> Track for T

§

fn track_allocations(self, token: AllocationGroupToken) -> Tracked<Self>

Instruments this type by attaching the given allocation group token, returning a Tracked wrapper. Read more
§

fn in_current_allocation_group(self) -> Tracked<Self>

Instruments this type by attaching the current allocation group, returning a Tracked wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more