简体   繁体   中英

Elixir: Nebulex (Caching library) error

I'm new to Elixir, trying to use Nebulex for making a simple local cache (Panda.Cache). I followed its tutorial but finally, by doing these commands:

data = %{id: 1, text: "hello"}
Mycache.set(data[:id], data)

I get this error:

** (ArgumentError) argument error
    (stdlib) :ets.lookup_element(Panda.Cache, :metadata, 2)
    (nebulex) lib/nebulex/adapters/local/metadata.ex:19: Nebulex.Adapters.Local.Metadata.get/1
    (nebulex) lib/nebulex/adapters/local.ex:177: Nebulex.Adapters.Local.set/4
    (panda) lib/panda/cache.ex:2: Panda.Cache.execute/2

Panda is the name of my Elixir app and Panda.Cache the name of the cache I'm trying to make.

Any help or solution would be appreciated. Thank you in advance.

Update:

Project folders and files are like:

panda
    config
       config.exs
    lib
       panda.ex
       panda
           application.ex
           cache.ex

config.exs file:

use Mix.Config

config :panda, Panda.Cache,
  adapter: Nebulex.Adapters.Local,
  gc_interval: 86_400 # 24 hrs

cache.ex file:

defmodule Panda.Cache do
  use Nebulex.Cache, otp_app: :panda
end

application.ex file:

defmodule Panda.Application do
  use Application

  def start(_type, _args) do
    import Supervisor.Spec

    children = [
      supervisor(Panda.Cache, [])
    ]

    opts = [strategy: :one_for_one, name: Panda.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

And, how I tried to use the cache in my code:

defmodule Panda do


  def mytest do
    data = %{id: 1, text: "hello"}

    Panda.Cache.set(data[:id], data)
  end

end

According to your code in GitHub ( https://github.com/ab00zar/FirstElixirCode ), in the mix.exs file you are missing the app module, currently it is like this:

def application do
  [
    extra_applications: [:logger]
  ]
end

But it should be like this:

def application do
  [
    extra_applications: [:logger],
    mod: {Panda.Application, []}
  ]
end

Because of this, your app (and supervision tree) it is not being started and the cache (Nebulex) is started as part of your supervision tree. Let me know if that works for you.

I tried the code you posted, but I cannot replicate the error.

When something like this happens I often do the following:

mix deps.clean --all
mix clean
mix deps.get
mix deps.compile
mix compile

and then I try again.

I hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM