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

Hooks should be provided access to the Worker state #1

Closed
whitfin opened this issue Apr 7, 2016 · 3 comments · Fixed by #20
Closed

Hooks should be provided access to the Worker state #1

whitfin opened this issue Apr 7, 2016 · 3 comments · Fixed by #20
Assignees

Comments

@whitfin
Copy link
Owner

whitfin commented Apr 7, 2016

As it currently stands, there is no way to call a cache from within a hook callback. This could prove valuable for synchronous hooks - i.e. you might want to retrieve a key quickly before it's removed.

As of v0.9.0, the Cachex interface accepts a worker and does not have to pass between procs to execute actions. This means that we can allow cache calls inside hooks using the worker, and everything should flow smoothly.

I'd imagine the best way to do this is to pass the worker into init/1 and allow the developer to choose whether to keep it around or not.

@whitfin whitfin self-assigned this Apr 7, 2016
@whitfin
Copy link
Owner Author

whitfin commented Apr 19, 2016

This is a bit of a pain, because hooks have to be built and started before the worker actually is... so the irony is the worker doesn't even exist during start up.

So... Need to think of another way around it. Considering an option to pass into a hook of requires_worker: true which causes the worker to ping { :worker, worker } on startup to allow the hook to configure itself accordingly.

This is kinda gross, but it does lend itself to backwards compatibility well.

defmodule MyModule.WorkerHook do
  use Cachex.Hook

  def init(args) when is_map(args) do
    { :ok, args }
  end

  def handle_modify({ :worker, worker }, state) do
    { :ok, Map.put(state, :worker, worker) }
  end

  def handle_notify(_action, state) do
    { :ok, IO.inspect(state) }
  end
end

@whitfin
Copy link
Owner Author

whitfin commented Apr 19, 2016

Ok I have this working now... I'll just neaten it up a bit and get a PR in. It's not the nicest interface, but I expect it to be less frequently used, so it shouldn't matter too much.

@whitfin
Copy link
Owner Author

whitfin commented Apr 19, 2016

Hit a bug with overriding handle_call/2 to give myself access to handle_modify/2 (namely that it breaks too easily). For this reason, just going to provide changes to handle_call/2 and document well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant