whitfin / cachex

A powerful caching library for Elixir with support for transactions, fallbacks and expirations
https://hexdocs.pm/cachex/
MIT License
1.6k stars 104 forks source link

Starting `Cachex` via `Supervisor.start_link` #383

Closed lucavenir closed 1 month ago

lucavenir commented 1 month ago

Hello there,

I really hope I'm not wasting anyone's time here, but I simply went and read the "welcome page/guide" of this (awesome!) package and I've got myself a papercut.

I read from the docs, in regards of starting Cachex in a supervision tree:

children = [
  {Cachex, [:my_cache]},     # with default options
  {Cachex, [:my_cache, []]}  # with custom options
]

So I try this in my phoenix application; I copy-paste the first line in my list of children:

children = [
  ...,
  {Cachex, [:my_cache]},
]

But, Supervisor.start_link gives this error:

** (Mix) Could not start application my_app: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: Cachex
    ** (EXIT) :invalid_name

Whereas, if I write my cache name like below, no error is raised on startup:

{Cachex, name: :my_cache},

Which makes sense, AFAIK supervisor wants a keyword list as a second argument of the tuple.

But... am I doing this right? Am I missing something?

lucavenir commented 1 month ago

By the way I've read #380 before opening this issue. I've updated my cachex dependency to match exactly 4.0.1, but the behavior is identical.

whitfin commented 1 month ago

Hi @lucavenir!

At a glance, yes, the docs for this are wrong and need to be updated. I can double check when I get a moment, just in case!

whitfin commented 1 month ago

@lucavenir instead of changing the docs, I decided to just make sure that the form shown in the README also works - I did that in #384. I also added checks to make sure this is tested going forward!

It should be released in v4.0.2 if you want to double check that it's all good for you now!

lucavenir commented 1 month ago

Oh, okay! I'll try this out ASAP.

If anyone's having this same problem, I am currently starting two different Cachex instances like so:

[
  Supervisor.child_spec(
    {Cachex, name: MyApp.Whatever.Module.One},
    id: MyApp.Whatever.Module.One
  ),
  Supervisor.child_spec(
    {Cachex, name: MyApp.Whatever.Module.Two},
    id: MyApp.Whatever.Module.Two
  )
]

I did this when trying to make it work: Supervisor.start_link/2 gives a nice error which shows you the different options.