Open rlipscombe opened 6 years ago
We had a similar situation, but at the end I decided to use sentinels in development also.
You could write a thin wrapper around Redix & RedixSentinel and dispatch based on some config. Probably some macro could make it easier
defmodule Redis
def command!()
if sentinel do
RedixSentinel.command!()
else
Redix.command!()
end
end
def start_link()
..
end
end
I will keep all the api except start_link same(it's documented and some of my projects depend on this). I am not convinced enough to add(or not sure how to go about it) such functionality in RedixSentinel yet.
@rlipscombe - if your still interested, I get around this by
Having a thin redis wrapper specific to my app (like @ananthakumaran said). I.e.
defmodule MyApp.RedisWrapper do
@redis_lib Application.get_env(:my_app, :redis_lib)
def get(conn, key) do
@redis_lib.command(conn, ["GET", key])
end
end
Then, in my mix configs I set which redis library to use.
In config/dev.exs
:
config :my_app, redis_lib: Redix
and in config/prod.exs
:
config :my_app, redis_lib: RedixSentinel
With regard to the calling start_link
, in my case I do it in a supervisor tree so I added a little function called child_tuple
to MyApp.RedisWrapper
that checks @redis_lib
and passes back the right tuple (I'd elaborate more with code, but my use case is super-specific which I think would distract).
Let's say I want to test something locally, and I only have a single redis instance, with no sentinel. Can I use this without a sentinel? Note that we are using sentinel in production, so I'd prefer to use
RedixSentinel
for that.I tried:
...but it failed with:
Related: It doesn't appear that I can mix
RedixSentinel.start_link
withRedix.command
, which would be the other way I'd try to make the interesting code agnostic about whether I'm using a sentinel or not (because I could then useRedix.start_link
when I knew I wasn't using a sentinel). That doesn't work either.