Using memcachex - requests to memcached hang

Hello. I am playing around with Memcachex library for accessing a Memcached server directly.

What I do - I define settings:

 config :my_app, MyApp.Memcache,
    host: "localhost",
    port: 11211

… and start the cache in Supervisor tree on application start:

 children = [
    [...]      
    {Memcache, [[{:name, {:local, MyApp.Memcache}}] ++ Application.get_env(:my_app, MyApp.Memcache, [])]}
  ]

and then somehwere else in the code, I try to access the cache:

# this call hangs indefinitely
res = Memcache.set({MyApp.Memcache, :local}, "live", 1)

Any suggestions on what I am doing wrong are welcome. I know that the server is working correctly, since I could actually write string values with the cream_ex library.

I see two issues with that code.

First, MemCache.start_link takes two sets of options:

  • the first is connection-specific options, like host and port
  • the second is passed directly to Connection.start_link and is explicitly called out as being for naming the process:

You’d need to change the line in your supervision tree to something like (but see below):

   # NOTE: don't use this line! Keep reading!
   {Memcache, [Application.get_env(:my_app, MyApp.Memcache, []), [{:name, {:local, MyApp.Memcache}}]]}

However, the second problem would appear immediately after you made that change: the name option expects a limited set of shapes, and {:local, atom()} isn’t one of them:

A bare atom like MyApp.MemCache is what should be used:

   # NOTE: this line should work!
   {Memcache, [Application.get_env(:my_app, MyApp.Memcache, []), [{:name, MyApp.Memcache}]]}
1 Like

Thanks - starting of memcachex within Supervisor was not documented and as you point out, I was starting it wrong. After making the changes you suggested, I access memcache like this:

Memcache.set(MyApp.Memcache, "testkey", "one")
# works
res = Memcache.get(MyApp.Memcache, "testkey")

And it works !