Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.blocking().invalidate(key) doesn't trigger eviction listener #242

Closed
fwojtan opened this issue Mar 16, 2023 · 1 comment · Fixed by #246
Closed

.blocking().invalidate(key) doesn't trigger eviction listener #242

fwojtan opened this issue Mar 16, 2023 · 1 comment · Fixed by #246
Assignees
Labels
bug Something isn't working
Milestone

Comments

@fwojtan
Copy link

fwojtan commented Mar 16, 2023

I've spotted that a moka::future::Cache doesn't fire its eviction listener if an entry is manually invalidated with the blocking op, but I can fire the eviction listener if using .invalidate_all() or the sync cache. The below examples should repro this behaviour.

#[tokio::test]
async fn invalidate_all_triggers_listener() {
    let cache = moka::future::Cache::builder()
        .eviction_listener_with_queued_delivery_mode(|k, v, c| {
            println!("evicted key: {}, value: {}, cause: {:?}", k, v, c);
        })
        .build();
    cache.get_with("foo", async { "all" }).await;
    cache.invalidate_all();
    tokio::time::sleep(Duration::from_secs(1)).await;
}

#[tokio::test]
async fn sync_triggers_listener() {
    let cache = moka::sync::Cache::builder()
        .eviction_listener(|k, v, c| {
            println!("evicted key: {}, value: {}, cause: {:?}", k, v, c);
        })
        .build();
    cache.insert("foo", "sync");
    cache.invalidate("foo");
}

#[tokio::test]
async fn blocking_invalidate_should_trigger_listener() {
    let cache = moka::future::Cache::builder()
        .eviction_listener_with_queued_delivery_mode(|k, v, c| {
            println!("evicted key: {}, value: {}, cause: {:?}", k, v, c);
        })
        .build();
    cache.get_with("foo", async { "blocking" }).await;
    cache.blocking().invalidate("foo");
    tokio::time::sleep(Duration::from_secs(1)).await;
}

For these tests the eviction handler only fires for the first two:

running 3 tests
evicted key: foo, value: sync, cause: Explicit
test sync_triggers_listener ... ok
evicted key: foo, value: all, cause: Explicit
test invalidate_all_triggers_listener ... ok
test blocking_invalidate_should_trigger_listener ... ok

My understanding of invalidate is that it is a manually triggered cache eviction, is that correct? If so I think this may be a bug.

@tatsuya6502 tatsuya6502 self-assigned this Mar 25, 2023
@tatsuya6502 tatsuya6502 added the bug Something isn't working label Mar 25, 2023
@tatsuya6502 tatsuya6502 added this to the v0.10.1 milestone Mar 25, 2023
@tatsuya6502
Copy link
Member

Thank you for reporting!

My understanding of invalidate is that it is a manually triggered cache eviction, is that correct? If so I think this may be a bug.

Yes, it is correct. .blocking().invalidate(key) should trigger the listener with RemovalCause::Explicit.

I will fix this bug as soon as I can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants