I’m working with Cachex to cache several types of data and I started running into problems with duplicate keys. I figured there are 2 solutions to that:
- Add prefixes to the keys.
- Start multiple instances of Cachex
Adding prefixes to provide a kind of “namespace” is simple enough, but it’s a bit smelly when using fetch
and you provide a callback: the callback function will receive as its argument the namespaced key that was looked for. In my case, I would have to strip off that namespace prefix before I could do anything with that key.
When I tried to start up multiple instance of Cachex in my application, I thought I could just add multiple child workers per https://hexdocs.pm/cachex/getting-started.html#starting-your-cache
children = [
supervisor(MyApp.Repo, []),
# Namespaces for cache instances
worker(Cachex, [:foo_cache, []]),
worker(Cachex, [:bar_cache, []]),
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
But trying to run the app in that configuration generates an error:
** (Mix) Could not start application my_app: MyApp.Application.start(:normal, []) returned an error: bad child specification, more than one child specification has the id: Cachex.
If using maps as child specifications, make sure the :id keys are unique.
If using a module or {module, arg} as child, use Supervisor.child_spec/2 to change the :id, for example:
children = [
Supervisor.child_spec({MyWorker, arg}, id: :my_worker_1),
Supervisor.child_spec({MyWorker, arg}, id: :my_worker_2)
]
I’ve tried adapting my arguments to the format suggested by that error message to something like this:
children = [
Supervisor.child_spec({Cachex, [:foo_cache, []]}, id: :cachex1),
Supervisor.child_spec({Cachex, [:bar_cache, []]}, id: :cachex2),
]
but that generates a different error:
** (Mix) Could not start application auth: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: :foo_cache
** (EXIT) :invalid_name
Is it bad practice to try and spin up multiple instances of Cachex? Should I just suck it up and deal with manually prefixing keys? Or can someone show me how to properly declare multiple Cachex instances in my supervisor?
Thanks!