-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
Feature request: Add an eviction_callback
parameter to the cache builder
#128
Comments
Hi. Yes, notification on evictions is on our road map ("feat: Notifications on eviction, etc."). I just moved it up to second on the road map list (from sixth). I will start to work on it in next week. |
FYI. I started to work on this via the following pull request: It is work in progress so still missing some notifications such as the one for expiration. But this unit test will illustrate how to use it. |
@tatsuya6502 I'm always excitedly curious to see what you do, so I took a peek at the draft. I noticed that your eviction listener differs slightly in execution semantics to Caffeine's. I thought it might help if I clarify the history and intent of our api, though freely ignore our semantics. The cache originally offered a A complaint is that sometimes the business logic requires key-based ordering of events that are performed synchronously with the operation. For example if removal deletes a local file then a race to add it back might find the file still exists and the listener leaves the cache in an inconsistent state (a cache entry but no file). Therefore, an ability to invoke the listener as part of the map operation was needed. Since It might be that in a new api you would want to register a Anyway, I hope that helps you in your api design! |
Thanks @ben-manes for sharing your experience and insight. I am learning lots from them! As you might have already seen, current implementation is similar (but not the same) to Caffeine's
and I noticed that unordered events for a key can cause the issue you said:
So I made the example in the doc to avoid the issue by generating a unique file name on every file creation. I was thinking to add some texts to explain why the file names have to be unique even for the same key.
I think it is a great idea to have blocking or non-blocking option per cache. I have a feeling that adding blocking option is not very hard, though I have to add per-key writer lock to some places, which could be tricky. I will give it a try now.
Avoiding a thread pool by default (for non-blocking notification and other housekeeping tasks like entry expiration) is something I wanted to do on Moka cache. I will try to implement it for notification now (hope it is easy enough), and will deffer for other housekeeping tasks to a near future release. |
HI @fbernier, Which cache are you using? ( I found that supporting blocking notification in If you are using EDIT |
hey sorry for the delay, my use-case with the eviction callback is currently with the |
Hi folks, thanks for your efforts on building such a good lib. 🥰 I'm also a user that needs this feature with |
Hi @fbernier,
It is great to hear! Thanks. |
Hi @MrCroxx,
Yes, the branch eviction-listener should be ready for you. I will add few more changes in next few days but it will be good enough now already. moka = { git = "https://github.com/moka-rs/moka", branch = "eviction-listener", features = ["future"] } To use the eviction listener, see the "Eviction Listener" example in the doc comment: https://github.com/moka-rs/moka/blob/eviction-listener/src/future/builder.rs#L55 You may have already read, but in coming release (v0.9.0), For example: Non-blocking notification mode This mode does not guarantee that the notifications for a specific key are ordered. (
Blocking notification mode This mode guarantees that the notifications for a specific key are ordered.
To avoid the issues in above non-blocking example, you could assign unique names to the external resource created at T2 and T5. (See the example in the doc) |
I had forgotten about async caches and synchronous removal listening in my earlier message. That’s a great reason to not support a generic synchronous listener for explicit removals. For evictions an in-flight future should probably be skipped so a synchronous callback is viable. That is how Caffeine gets around this, where futures are ineligible for eviction until complete. |
Sorry for some delay. I could not find enough time for working on this during weekdays. But finally, I think #145 is now ready for merge. I will self-review it in tomorrow morning (I live in China mainland UTC+0800 and its 10pm now) and will merge it if I do not see any issue. I implemented the blocking and non-blocking modes to the I could not implement non-worker threads version of the immediate/queued modes just because lack of time. So currently both rely on the worker threads. |
Closing this issue as I merged PR #145 into the I will run some pre-release testing, and if everything goes well, I will publish Moka v0.9.0 to crates.io. |
I have published Moka v0.9.0 to crates.io. Documents:
|
Thanks a lot! Happy to hear that and can't wait to try! 🥰 |
Hey there, first and foremost thanks for moka!
I currently have this use case where I have other data structures I need to keep in sync with a moka cache. One way I though of doing this is the cache builder could support a new
eviction_callback
method which takes a closure that is called every time there is a cache eviction, with ideally both the key and value as parameters to the closure. Do you think that's feasible and if so, is this something you'd be interested in this being added to moka?I envision something like:
or
The text was updated successfully, but these errors were encountered: