How cache works in redis?

So I’ve written a gensrver which connects to redis server and then it returns me pid. Then I take that pid to set a key and value which is working right now. Also when I call get it will give that particular value like this

iex(3)> {:ok, pid} = Database.start_link("redis://localhost:6379/3")
[info] connect to url redis://localhost:6379/3
{:ok, #PID<0.467.0>}
iex(4)> Database.set(pid, "k", "v")                                 
{:ok, {:ok, "OK"}}
iex(5)> Database.get(pid, "k")     
{:ok, {:ok, "v"}}

So After this if I go to redis cli will it give me same key and value there?

def start_link(url) do
    GenServer.start_link(__MODULE__, {url})

  def init({url}) do"connect to url #{url}");
    case Redix.start_link(url) do
      {:ok, conn} -> {:ok, conn}
      {:error, err} -> {:error, err}

  def check(pid) do, :check)

  def handle_call(:check, _from, state) do
    checking = Redix.command!(state, ["PING"])
    {:reply, checking, state}

  def get(pid, key) do, {:get, key})

  def handle_call({:get, key}, _from, state) do
    reply = Redix.command(state, ["GET", key])
    {:reply, {:ok, reply}, state}

  def set(pid, key, value) do, {:set, key, value})

  def handle_call({:set, key, value}, _from, state) do
    reply = Redix.command(state, ["SET", key, value])
    {:reply, {:ok, reply}, state}

  def userone(pid, id) do, {:userone, id})

  def list(pid) do, :list)

  def handle_call(:list, _from, state) do
    my_models = Accounts.list_users()

    {:reply, my_models, state}

  def handle_call({:userone, id}, _from, state) do
    user = Accounts.get_user!(id)
    {:reply, user, state}

Just trying might have been a lot quicker than asking…

Yes. If you connect to the same redis instance and use the same database, then elixir and redis CLI should get the same results.

What does same database mean in this case? the redis cache? or the postgres/mysql database?

May I ask why you’re wrapping redix inside a gen_server? That seems highly ill-advised.


Because I have to use supervisor to supervise this later. Just testing

Redis database.

1 Like

You should supervise redix directly. In fact the way you’re doing it now, redix won’t be supervised.

What do you suggest? Can you give me some example using my above cases

I don’t know how to suggest ‘dont make your life complicated’. First thing is, if you don’t know if you need a genserver, you probably don’t need a genserver. Second thing is, if you think you need a genserver and you’re new to erlang/elixir, you probably don’t need a genserver.

So my point is that you’re building a gen server around redix, which is already a gen_server. The way your code works now, redix will be linked to a (presumably supervised) gen_server, but that is not the same as being supervised itself.

If you need to rewrite redix calls into a less abstracted form that suits your business case, you should use a normal module, not a module with a gen_server behaviour.


Thanks man. But you can suggest me anything. It will be good for me

Thanks It worked!

1 Like

Nice to see that you made good progress with your exercise :slight_smile:

Yes, if you set a key in Redis to a value, and then you get it, you will get back the same value, no matter if from Elixir or from the Redis cli (assuming you are reading from the same Redis instance, and nothing wrote or deleted the same key).

1 Like

Actually I wasn’t using the same url which my current redis is working on. Now I’ve just used this “redis://” and it worked.

Thanks man. I hope I was clear this time :sweat_smile:

1 Like