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!

First 5 of 5 Posts! Switch mode

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?

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.

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

OvermindDL1

OvermindDL1

Lol, wow, nice! ^.^

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement