moka/
notification.rs

1//! Common data types for notifications.
2
3#[cfg(feature = "sync")]
4pub(crate) mod notifier;
5
6use std::{future::Future, pin::Pin, sync::Arc};
7
8/// A future returned by an eviction listener.
9///
10/// You can use the [`boxed` method][boxed-method] of `FutureExt` trait to convert a
11/// regular `Future` object into `ListenerFuture`.
12///
13/// [boxed-method]: ../future/trait.FutureExt.html#method.boxed
14pub type ListenerFuture = Pin<Box<dyn Future<Output = ()> + Send>>;
15
16#[cfg(feature = "sync")]
17pub(crate) type EvictionListener<K, V> =
18    Arc<dyn Fn(Arc<K>, V, RemovalCause) + Send + Sync + 'static>;
19
20#[cfg(feature = "future")]
21pub(crate) type AsyncEvictionListener<K, V> =
22    Box<dyn Fn(Arc<K>, V, RemovalCause) -> ListenerFuture + Send + Sync + 'static>;
23
24// NOTE: Currently, dropping the cache will drop all entries without sending
25// notifications. Calling `invalidate_all` method of the cache will trigger
26// the notifications, but currently there is no way to know when all entries
27// have been invalidated and their notifications have been sent.
28
29/// Indicates the reason why a cached entry was removed.
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31pub enum RemovalCause {
32    /// The entry's expiration timestamp has passed.
33    Expired,
34    /// The entry was manually removed by the user.
35    Explicit,
36    /// The entry itself was not actually removed, but its value was replaced by
37    /// the user.
38    Replaced,
39    /// The entry was evicted due to size constraints.
40    Size,
41}
42
43impl RemovalCause {
44    pub fn was_evicted(&self) -> bool {
45        matches!(self, Self::Expired | Self::Size)
46    }
47}