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

On-change handler for write-through decorators #165

Closed
tpitale opened this issue Aug 31, 2022 · 5 comments
Closed

On-change handler for write-through decorators #165

tpitale opened this issue Aug 31, 2022 · 5 comments

Comments

@tpitale
Copy link

tpitale commented Aug 31, 2022

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.

@tpitale tpitale changed the title On-change handler for write-through annotations On-change handler for write-through decorators Aug 31, 2022
@tpitale
Copy link
Author

tpitale commented Aug 31, 2022

Or maybe there is a way to do this already, and I just haven't found it?

@cabol
Copy link
Owner

cabol commented Aug 31, 2022

New features are always welcome! According to my understanding, the :on_change option would be for the cache_put annotation, and what you want is only to put/update the new value in the cache if changed, is that right? If so, keep in mind that will require an extra operation for retrieving the current value from the cache, so that it can be compared and based on that decide whether it should be updated or not. Now, currently, the cache_put always puts the new value in the cache, but that is a single write operation. What I'd ask is if maybe you can elaborate more on the idea to understand better, how it would work, and even give some examples.

@tpitale
Copy link
Author

tpitale commented Aug 31, 2022

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.

@cabol
Copy link
Owner

cabol commented Aug 31, 2022

IMHO, it looks more like a custom/specific use case, in which I'd use c:get_and_update rather than the cache_put annotation. Like:

# 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!

@tpitale
Copy link
Author

tpitale commented Aug 31, 2022

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!

@tpitale tpitale closed this as not planned Won't fix, can't repro, duplicate, stale Aug 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants