-
Notifications
You must be signed in to change notification settings - Fork 75
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
On-change handler for write-through decorators #165
Comments
Or maybe there is a way to do this already, and I just haven't found it? |
New features are always welcome! According to my understanding, the |
I think it would leave cache_put the same way if no on_change function was passed in the annotation. Then if one was passed, as you said, it would first read the value. Otherwise, cache_put would behave the same (or not, it could not write if the value hasn't changed). But mostly, the value would be to call the on_change function with old and new value when they don't match. Wanting to use this to trigger some cascading recalculations when one of the values relevant to that calculations changes within the cache. So, like we get Kafka messages with some data, we write that to the cache, we recalculate something elsewhere. This would allow us to only recalculate if a change happens. |
IMHO, it looks more like a custom/specific use case, in which I'd use c:get_and_update rather than the # The function called when you get the Kafka message
def some_function(key, new_value) do
MyCache.get_and_update(key, fn current_value ->
maybe_recalculations(current_value, new_value)
{current_value, new_value}
end)
end
def maybe_recalculations(value, value) do
# same value, so maybe skip it?
:noop
end
def maybe_recalculations(old_value, new_value) do
# run cascading recalculations ...
end Anyway, this is how I would approach this, perhaps I'm missing something, let me know your thoughts! |
That works for me. I thought it might be nice to have this option with the annotation. But I'm good with this route. Thanks! |
If I were to submit a PR to add support for something like an "on-change" handler to the write-through annotation (thinking
cache_put
), would that be something you'd be open to adding as a feature?It feels like something others might want to use. If not, I can probably just add it in my own codebase.
The text was updated successfully, but these errors were encountered: