foyer_common/
event.rs

1// Copyright 2025 foyer Project Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::code::{Key, Value};
16
17/// Event identifier.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum Event {
20    /// Cache eviction on insertion.
21    Evict,
22    /// Cache replacement on insertion.
23    Replace,
24    /// Cache remove.
25    Remove,
26    /// Cache clear.
27    Clear,
28}
29
30/// Trait for the customized event listener.
31pub trait EventListener: Send + Sync + 'static {
32    /// Associated key type.
33    type Key;
34    /// Associated value type.
35    type Value;
36
37    /// Called when a cache entry leaves the in-memory cache with the reason.
38    #[expect(unused_variables)]
39    fn on_leave(&self, reason: Event, key: &Self::Key, value: &Self::Value)
40    where
41        Self::Key: Key,
42        Self::Value: Value,
43    {
44    }
45}