How to write repository class with elixir

Hi everyone,

I have a question regarding how to implement repository class in elixir. In this case I want to set/get from redis cache. So I intend to write the module as follow

RedisRepository
getOrders(filter)
getOrder(orderId)
addOrder(order)
deleteOrder(order)

and I intend to use Redix lib to talk to Redis and Redix require that you init a process (start_link(x,y,z)) and keep the connection(pid) so that we can use that conn to execute redis commands.

My problem is I don’t know where/how to init the Redix process and store it somewhere, so that I can use that connection in RedisRepository module to set/get data from redis.
I would appreciate if someone can help recommend an approach that adhere to otp/elixir best practices
Thank you so much

You may find documentation on a real world usage…

https://hexdocs.pm/redix/real-world-usage.html

1 Like

Start it as a named process in your supervision tree, then you can use that name instead of the pid in your repository module.

children = [
  {Redix, host: "redix.myapp.com", name: :redix}
]
1 Like