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

need to manually run_pending_tasks() to trigger eviction_listener callback. #349

Closed
unikzforce opened this issue Nov 20, 2023 · 7 comments
Closed
Assignees
Labels
question Further information is requested

Comments

@unikzforce
Copy link

unikzforce commented Nov 20, 2023

Hello,

in the end of my main() function i have this while loop:

    let user_mac_table_clone_3 = Arc::clone(&user_mac_table_arc);
    while running.load(Ordering::SeqCst) {
        mgr.poll(Duration::from_secs(5))?;
        user_mac_table_clone_3.as_ref().i.run_pending_tasks();
        println!("Content of the user_mac_table, {:?}", user_mac_table_clone_3.as_ref().i.entry_count());
        for (key, value) in user_mac_table_clone_3.as_ref().iter() {
            // println!("the Key is {}, the value is {}", key.clone().as_ref(), value)
            println!("the Key is {:?}, the value is {:?}, the last registered time is {:?}", key.mac ,value.interface_index, value.timestamp)
        }
    }

it will wait 5 seconds for something and then after that it will print the content of the cache,
the problem is that if i don't invoke run_pending_tasks() the eviction_listener would not be called,

but i want the eviction_listener() to be called exactly at the time of eviction, not some time later,

can you please help me to understand what should i do?

@tatsuya6502 tatsuya6502 self-assigned this Nov 20, 2023
@tatsuya6502 tatsuya6502 added the question Further information is requested label Nov 20, 2023
@tatsuya6502
Copy link
Member

Hi. It does not have to be run_pending_tasks method, but you need to call some of the cache methods such as get, get_with, insert, or remove to drive eviction_listener.

Before v0.12.0, Moka had its own global thread pool to periodically run pending tasks. Some users did not like it, so we removed it. You will find more details here when pending tasks (internal maintenance tasks) will be executed: https://github.com/moka-rs/moka/blob/main/MIGRATION-GUIDE.md#the-maintenance-tasks

but i want the eviction_listener() to be called exactly at the time of eviction, not some time later,

can you please help me to understand what should i do?

You can spawn a thread and make it to call run_pending_tasks with some interval (e.g. 0.1 secs). If you need some code samples, I could write one for you.

@unikzforce
Copy link
Author

unikzforce commented Nov 20, 2023

aaah now I get it, thanks a lot, i would really appreciate if you add an example in the examples folder and point to it in the readme

@unikzforce
Copy link
Author

one more thing, could you please kindly think of a way to pass exact eviction_time?

so maybe we could have two variants of eviction_listener functions, one could be executed without evection time like what it is now for the sake of backward compatibility, and another one could be executed with exact time that the eviction has really happened.

in my eviction_listener i have to check if the eviction has happened after certain time or not, so having the exact eviction time could help me to have a more solid eviction_listener function.

@unikzforce
Copy link
Author

Is eviction_time impossible?

@tatsuya6502
Copy link
Member

Hi. I will not be able to work on moka for next few weeks because I am in a hospital now. #352. (I only have a smart phone now)

eviction_time is possible but maybe not necessary(?) as it is guaranteed that the eviction listener should be called immediately after the eviction. Eviction is delayed until pending tasks are procecced, but once these tasks are processed and a cached entry is evicted, the eviction listener should be called immediately. So eviction_time will be a few nano/micro seconds before when the eviction listener is called.

There will be one exception though. OS can preempt a thread at any time and it can cause some (a few milliseconds?) delay before calling eviction listener. If your application cannot tolerate this delay, eviction_time will be needed.

The following doc was for v0.11.3. It explains how the immediate delivery mode worked in v0.11. moka v0.12 always uses the immediate mode, so the doc is valid for v0.12 except v0.12 does not have background eviction threads.
https://docs.rs/moka/0.11.3/moka/sync/struct.Cache.html#delivery-modes-for-eviction-listener

@unikzforce
Copy link
Author

😔 i hope you get well as soon as possible💚💚

@unikzforce
Copy link
Author

for my case it's not super important. i was just looking at it as an idea for a possible future improvement 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants