Skip to content

Commit 05fdf06

Browse files
authored
Merge pull request #246 from moka-rs/blocking-invalidate-should-trigger-listener
`future::Cache`'s `.blocking().invalidate(key)` should trigger the eviction listener
2 parents 0acb76b + a5f9c61 commit 05fdf06

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Version 0.10.1
44

5+
### Fixed
6+
7+
- Fixed a bug that `future` cache's `blocking().invalidate(key)` method does not
8+
trigger the eviction listener. ([#242][gh-issue-0242])
9+
510
### Changed
611

712
- Now `sync` and `future` caches will not cache anything when the max capacity is set
@@ -585,6 +590,7 @@ The minimum supported Rust version (MSRV) is now 1.51.0 (2021-03-25).
585590
[gh-Swatinem]: https://github.com/Swatinem
586591
[gh-tinou98]: https://github.com/tinou98
587592

593+
[gh-issue-0242]: https://github.com/moka-rs/moka/issues/242/
588594
[gh-issue-0230]: https://github.com/moka-rs/moka/issues/230/
589595
[gh-issue-0212]: https://github.com/moka-rs/moka/issues/212/
590596
[gh-issue-0207]: https://github.com/moka-rs/moka/issues/207/

src/future/cache.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,9 @@ where
12691269
{
12701270
let hash = self.base.hash(key);
12711271
if let Some(kv) = self.base.remove_entry(key, hash) {
1272+
if self.base.is_removal_notifier_enabled() {
1273+
self.base.notify_invalidate(&kv.key, &kv.entry)
1274+
}
12721275
let op = WriteOp::Remove(kv);
12731276
let now = self.base.current_time_from_expiration_clock();
12741277
let hk = self.base.housekeeper.as_ref();
@@ -1963,7 +1966,21 @@ mod tests {
19631966

19641967
#[test]
19651968
fn basic_single_blocking_api() {
1966-
let mut cache = Cache::new(3);
1969+
// The following `Vec`s will hold actual and expected notifications.
1970+
let actual = Arc::new(Mutex::new(Vec::new()));
1971+
let mut expected = Vec::new();
1972+
1973+
// Create an eviction listener.
1974+
let a1 = Arc::clone(&actual);
1975+
// We use non-async mutex in the eviction listener (because the listener
1976+
// is a regular closure).
1977+
let listener = move |k, v, cause| a1.lock().push((k, v, cause));
1978+
1979+
// Create a cache with the eviction listener.
1980+
let mut cache = Cache::builder()
1981+
.max_capacity(3)
1982+
.eviction_listener_with_queued_delivery_mode(listener)
1983+
.build();
19671984
cache.reconfigure_for_testing();
19681985

19691986
// Make the cache exterior immutable.
@@ -1988,24 +2005,30 @@ mod tests {
19882005

19892006
// "d" should not be admitted because its frequency is too low.
19902007
cache.blocking().insert("d", "david"); // count: d -> 0
2008+
expected.push((Arc::new("d"), "david", RemovalCause::Size));
19912009
cache.sync();
19922010
assert_eq!(cache.get(&"d"), None); // d -> 1
19932011

19942012
cache.blocking().insert("d", "david");
2013+
expected.push((Arc::new("d"), "david", RemovalCause::Size));
19952014
cache.sync();
19962015
assert_eq!(cache.get(&"d"), None); // d -> 2
19972016

19982017
// "d" should be admitted and "c" should be evicted
19992018
// because d's frequency is higher than c's.
20002019
cache.blocking().insert("d", "dennis");
2020+
expected.push((Arc::new("c"), "cindy", RemovalCause::Size));
20012021
cache.sync();
20022022
assert_eq!(cache.get(&"a"), Some("alice"));
20032023
assert_eq!(cache.get(&"b"), Some("bob"));
20042024
assert_eq!(cache.get(&"c"), None);
20052025
assert_eq!(cache.get(&"d"), Some("dennis"));
20062026

20072027
cache.blocking().invalidate(&"b");
2028+
expected.push((Arc::new("b"), "bob", RemovalCause::Explicit));
20082029
assert_eq!(cache.get(&"b"), None);
2030+
2031+
verify_notification_vec(&cache, actual, &expected);
20092032
}
20102033

20112034
#[tokio::test]

0 commit comments

Comments
 (0)