Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,26 @@ pub trait Lifecycle<Key, Val> {
fn before_evict(&self, state: &mut Self::RequestState, key: &Key, val: &mut Val) {}

/// Called when an item is evicted.
fn on_evict(&self, state: &mut Self::RequestState, key: Key, val: Val);
#[deprecated(
since = "0.6.22",
note = "Use `on_evict_hot` or `on_evict_cold` instead, depending on the desired semantics. This method will still be called by default to preserve backwards compatibility, but it won't be called if either of the new methods are implemented."
)]
#[allow(unused_variables)]
fn on_evict(&self, state: &mut Self::RequestState, key: Key, val: Val) {}
Comment on lines +203 to +212

/// Called when an item is evicted from the cold queue.
#[inline]
fn on_evict_cold(&self, state: &mut Self::RequestState, key: Key, val: Val) {
#[allow(deprecated)]
self.on_evict(state, key, val)
}

/// Called when an item is evicted from the hot queue.
#[inline]
fn on_evict_hot(&self, state: &mut Self::RequestState, key: Key, val: Val) {
#[allow(deprecated)]
self.on_evict(state, key, val)
}
Comment on lines +226 to +235

/// Called after a request finishes, e.g.: insert, replace.
///
Expand Down
21 changes: 15 additions & 6 deletions src/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,8 @@ impl<
if self.num_non_resident > self.capacity_non_resident {
self.advance_ghost();
}
self.lifecycle.on_evict(lcs, evicted.key, evicted.value);
self.lifecycle
.on_evict_cold(lcs, evicted.key, evicted.value);
return true;
}
}
Expand Down Expand Up @@ -835,7 +836,7 @@ impl<
unsafe { core::hint::unreachable_unchecked() };
};
self.hot_head = next;
self.lifecycle.on_evict(lcs, evicted.key, evicted.value);
self.lifecycle.on_evict_hot(lcs, evicted.key, evicted.value);
self.map_remove(hash, idx);
}
return true;
Expand Down Expand Up @@ -920,7 +921,15 @@ impl<
} else if evicted_weight != 0 && weight == 0 {
*list_head = self.entries.unlink(idx);
}
self.lifecycle.on_evict(lcs, evicted.key, evicted.value);
match enter_state {
ResidentState::Hot => {
self.lifecycle.on_evict_hot(lcs, evicted.key, evicted.value)
}
ResidentState::Cold => {
self.lifecycle
.on_evict_cold(lcs, evicted.key, evicted.value)
}
}
}
Entry::Ghost(_) => {
self.weight_hot += weight;
Expand Down Expand Up @@ -1052,7 +1061,7 @@ impl<
) -> Result<(), Val> {
self.entries.remove(placeholder.idx());
self.map_remove(placeholder.hash(), placeholder.idx());
self.lifecycle.on_evict(lcs, key, value);
self.lifecycle.on_evict_hot(lcs, key, value);
Ok(())
}

Expand Down Expand Up @@ -1122,13 +1131,13 @@ impl<
// Make sure to remove any existing entry
if let Some((idx, _)) = self.search_resident(hash, &key) {
if let Some((ek, ev)) = self.remove_internal(hash, idx) {
self.lifecycle.on_evict(lcs, ek, ev);
self.lifecycle.on_evict_hot(lcs, ek, ev);
}
}
if matches!(strategy, InsertStrategy::Replace { .. }) {
return Err((key, value));
}
self.lifecycle.on_evict(lcs, key, value);
self.lifecycle.on_evict_hot(lcs, key, value);
Ok(())
}

Expand Down
Loading