Trait moka::Expiry

source ·
pub trait Expiry<K, V> {
    // Provided methods
    fn expire_after_create(
        &self,
        key: &K,
        value: &V,
        current_time: Instant
    ) -> Option<Duration> { ... }
    fn expire_after_read(
        &self,
        key: &K,
        value: &V,
        current_time: Instant,
        current_duration: Option<Duration>,
        last_modified_at: Instant
    ) -> Option<Duration> { ... }
    fn expire_after_update(
        &self,
        key: &K,
        value: &V,
        current_time: Instant,
        current_duration: Option<Duration>
    ) -> Option<Duration> { ... }
}
Available on crate features sync or future only.
Expand description

Calculates when cache entries expire. A single expiration time is retained on each entry so that the lifetime of an entry may be extended or reduced by subsequent evaluations.

Expiry trait provides three methods. They specify the expiration time of an entry by returning a Some(duration) until the entry expires:

The default implementations are provided that return None (no expiration) or current_duration: Option<Instant> (not modify the current expiration time). Override some of them as you need.

Provided Methods§

source

fn expire_after_create( &self, key: &K, value: &V, current_time: Instant ) -> Option<Duration>

Specifies that the entry should be automatically removed from the cache once the duration has elapsed after the entry’s creation. This method is called for cache write methods such as insert and get_with but only when the key was not present in the cache.

Parameters
  • key — A reference to the key of the entry.
  • value — A reference to the value of the entry.
  • current_time — The current instant.
Returning None
  • Returning None indicates no expiration for the entry.
  • The default implementation returns None.
Notes on time_to_live and time_to_idle policies

When the cache is configured with time_to_live and/or time_to_idle policies, the entry will be evicted after the earliest of the expiration time returned by this expiry, the time_to_live and time_to_idle policies.

source

fn expire_after_read( &self, key: &K, value: &V, current_time: Instant, current_duration: Option<Duration>, last_modified_at: Instant ) -> Option<Duration>

Specifies that the entry should be automatically removed from the cache once the duration has elapsed after its last read. This method is called for cache read methods such as get and get_with but only when the key is present in the cache.

Parameters
  • key — A reference to the key of the entry.
  • value — A reference to the value of the entry.
  • current_time — The current instant.
  • current_duration — The remaining duration until the entry expires.
  • last_modified_at — The instant when the entry was created or updated.
Returning None or current_duration
  • Returning None indicates no expiration for the entry.
  • Returning current_duration will not modify the expiration time.
  • The default implementation returns current_duration (not modify the expiration time)
Notes on time_to_live and time_to_idle policies

When the cache is configured with time_to_live and/or time_to_idle policies, then:

  • The entry will be evicted after the earliest of the expiration time returned by this expiry, the time_to_live and time_to_idle policies.
  • The current_duration takes in account the time_to_live and time_to_idle policies.
source

fn expire_after_update( &self, key: &K, value: &V, current_time: Instant, current_duration: Option<Duration> ) -> Option<Duration>

Specifies that the entry should be automatically removed from the cache once the duration has elapsed after the replacement of its value. This method is called for cache write methods such as insert but only when the key is already present in the cache.

Parameters
  • key — A reference to the key of the entry.
  • value — A reference to the value of the entry.
  • current_time — The current instant.
  • current_duration — The remaining duration until the entry expires.
Returning None or current_duration
  • Returning None indicates no expiration for the entry.
  • Returning current_duration will not modify the expiration time.
  • The default implementation returns current_duration (not modify the expiration time)
Notes on time_to_live and time_to_idle policies

When the cache is configured with time_to_live and/or time_to_idle policies, then:

  • The entry will be evicted after the earliest of the expiration time returned by this expiry, the time_to_live and time_to_idle policies.
  • The current_duration takes in account the time_to_live and time_to_idle policies.

Implementors§