pub enum Event {
Insert(EventData),
Remove(EventData),
Clear,
}
Expand description
Represents different types of cache events.
Events are sent through a channel when cache operations occur, allowing external observers to react to cache changes.
§Examples
use quickleaf::{Event, EventData};
use quickleaf::Cache;
use quickleaf::valu3::traits::ToValueBehavior;
use std::sync::mpsc::channel;
let (tx, rx) = channel();
let mut cache = Cache::with_sender(5, tx);
// Insert an item
cache.insert("user_123", "session_data");
// Receive the insert event
if let Ok(event) = rx.try_recv() {
match event {
Event::Insert(data) => {
println!("Inserted: {} = {}", data.key, data.value);
assert_eq!(data.key, "user_123");
},
Event::Remove(data) => {
println!("Removed: {} = {}", data.key, data.value);
},
Event::Clear => {
println!("Cache cleared");
},
}
}
Variants§
Insert(EventData)
An item was inserted into the cache.
§Examples
use quickleaf::{Event, EventData};
use quickleaf::valu3::traits::ToValueBehavior;
let event = Event::insert("key".to_string(), "value".to_value());
match event {
Event::Insert(data) => {
assert_eq!(data.key, "key");
assert_eq!(data.value, "value".to_value());
},
_ => panic!("Expected insert event"),
}
Remove(EventData)
An item was removed from the cache.
§Examples
use quickleaf::{Event, EventData};
use quickleaf::valu3::traits::ToValueBehavior;
let event = Event::remove("key".to_string(), "value".to_value());
match event {
Event::Remove(data) => {
assert_eq!(data.key, "key");
assert_eq!(data.value, "value".to_value());
},
_ => panic!("Expected remove event"),
}
Clear
The entire cache was cleared.
§Examples
use quickleaf::Event;
let event = Event::clear();
match event {
Event::Clear => println!("Cache was cleared"),
_ => panic!("Expected clear event"),
}
Implementations§
Source§impl Event
impl Event
Sourcepub fn insert(key: String, value: Value) -> Self
pub fn insert(key: String, value: Value) -> Self
Creates a new insert event.
§Examples
use quickleaf::Event;
use quickleaf::valu3::traits::ToValueBehavior;
let event = Event::insert("user_session".to_string(), "active".to_value());
match event {
Event::Insert(data) => {
assert_eq!(data.key, "user_session");
assert_eq!(data.value, "active".to_value());
},
_ => panic!("Expected insert event"),
}
Sourcepub fn remove(key: String, value: Value) -> Self
pub fn remove(key: String, value: Value) -> Self
Creates a new remove event.
§Examples
use quickleaf::Event;
use quickleaf::valu3::traits::ToValueBehavior;
let event = Event::remove("expired_key".to_string(), "old_data".to_value());
match event {
Event::Remove(data) => {
assert_eq!(data.key, "expired_key");
assert_eq!(data.value, "old_data".to_value());
},
_ => panic!("Expected remove event"),
}
Trait Implementations§
impl StructuralPartialEq for Event
Auto Trait Implementations§
impl Freeze for Event
impl RefUnwindSafe for Event
impl Send for Event
impl Sync for Event
impl Unpin for Event
impl UnwindSafe for Event
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more