use std::sync::Arc;
use tokio::sync::broadcast;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CacheEvent {
Hit {
key: Arc<str>,
stale: bool,
},
Miss {
key: Arc<str>,
},
Set {
key: Arc<str>,
},
Remove {
key: Arc<str>,
},
Expire {
key: Arc<str>,
},
FactorySuccess {
key: Arc<str>,
},
FactoryError {
key: Arc<str>,
message: String,
},
FactorySyntheticTimeout {
key: Arc<str>,
},
FailSafeActivate {
key: Arc<str>,
},
EagerRefresh {
key: Arc<str>,
},
BackgroundFactorySuccess {
key: Arc<str>,
},
BackgroundFactoryError {
key: Arc<str>,
message: String,
},
RemoveByTag {
tag: String,
},
Clear,
Eviction {
key: Arc<str>,
},
CircuitBreakerChange {
component: CircuitComponent,
closed: bool,
},
SerializationError {
key: Arc<str>,
message: String,
},
DeserializationError {
key: Arc<str>,
message: String,
},
MessagePublished {
key: Arc<str>,
},
MessageReceived {
key: Arc<str>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitComponent {
Distributed,
Backplane,
}
#[derive(Debug, Clone)]
pub struct Events {
sender: broadcast::Sender<CacheEvent>,
}
impl Events {
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
let (sender, _) = broadcast::channel(capacity.max(1));
Self { sender }
}
#[must_use]
pub fn subscribe(&self) -> broadcast::Receiver<CacheEvent> {
self.sender.subscribe()
}
pub fn emit(&self, event: CacheEvent) {
let _ = self.sender.send(event);
}
}
impl Default for Events {
fn default() -> Self {
Self::with_capacity(256)
}
}