fireproofsocks

fireproofsocks

Defining multiple instances of Cachex?

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:

  1. Add prefixes to the keys.
  2. 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!

Marked As Solved

drl123

drl123

Had this problem too and Whitfin helped me get it going. You need to give the workers an id after the options that matches the cache name. Do something like this:

worker(Cachex, [:temp_cache, [expiration: expiration(default: :timer.minutes(10))]], id: :temp_cache),
worker(Cachex, [:main_cache, []], id: :main_cache),

This worked fine in my app. I needed on with a fixed/short TTL and another that was long-lived.

Also Liked

fireproofsocks

fireproofsocks

Ah, I think figured it out:

worker(Cachex, [:foo_cache, []], id: :cachex_1),
worker(Cachex, [:bar_cache, []], id: :cachex_2),

But is that a good idea?

OvermindDL1

OvermindDL1

It’s a fine idea. I have like 4 cache’s in my big project. ^.^;

factoryd

factoryd

I have like at least 12. haha

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement