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

Setting number of segments will disable notifications #207

Closed
milenkovicm opened this issue Dec 7, 2022 · 2 comments · Fixed by #215
Closed

Setting number of segments will disable notifications #207

milenkovicm opened this issue Dec 7, 2022 · 2 comments · Fixed by #215
Assignees
Labels
bug Something isn't working
Milestone

Comments

@milenkovicm
Copy link

with latest moka 0.9.6 setting segments will disable notifications.

simple example to reproduce:

moka = { version = "0.9", features = [ "sync" ] }
use moka::sync::Cache;
use std::time::Duration;

#[tokio::main]
async fn main() {

    let notification_configuration = moka::notification::Configuration::builder()
        .delivery_mode(moka::notification::DeliveryMode::Queued)
        .build();

    let listener = move |key, _value, _cause|  {
        println!("--> {}", key)
    };

    let cache = Cache::builder()
        .time_to_idle(Duration::from_secs(3))
        .eviction_listener_with_conf(listener, notification_configuration)
        .name("tracked_sessions")
        .segments(1) 
        .build();


    cache.insert("key".to_string(), "value 0".to_string());
    cache.insert("key".to_string(), "value 1".to_string());
    cache.insert("key".to_string(), "value 2".to_string());
    tokio::time::sleep(Duration::from_secs(3)).await;
    cache.insert("key".to_string(), "value 3".to_string());
    tokio::time::sleep(Duration::from_secs(10)).await;    
}

Will print nothing, but removing .segments(1) will properly print/show four notifications.

at it looks, number of segments does not make difference.

@tatsuya6502 tatsuya6502 added the bug Something isn't working label Dec 8, 2022
@tatsuya6502 tatsuya6502 self-assigned this Dec 8, 2022
@tatsuya6502
Copy link
Member

Thank you for reporting the bug. This seems a simple mistake; calling segment method will reset the eviction listener, etc.

https://github.com/moka-rs/moka/blob/v0.9.6/src/sync/builder.rs#L117-L119

        CacheBuilder {
            name: self.name,
            max_capacity: self.max_capacity,
            initial_capacity: self.initial_capacity,
            num_segments: Some(num_segments),
            weigher: None,
            eviction_listener: None,  // <=
            eviction_listener_conf: None,

As a workaround, please call segment at the very beginning of the method chain:

let cache = Cache::builder()
        .segments(1)   // Move up to here.
        .time_to_idle(Duration::from_secs(3))
        .eviction_listener_with_conf(listener, notification_configuration)
        .name("tracked_sessions")
        .build();

@tatsuya6502
Copy link
Member

Closing. This bug has been addressed by #215 for upcoming v0.9.7 release.

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