Search code examples
rubyherokumemcachedrackdalli

How to specify memcache server to Rack::Session::Memcache?


I'm trying to configure my Rack app to use Memcache for sessions with Rack::Session::Memcache

How do I give it the options (such as server, username and password)?

Presently I have

use Rack::Session::Memcache

But I get the error

in `initialize': No memcache servers (RuntimeError)

Heroku has put the config in environment variables

MEMCACHE_PASSWORD:           
MEMCACHE_SERVERS:            
MEMCACHE_USERNAME:           

I know I can get these in Ruby with ENV['MEMCACHE_PASSWORD'] but I don't know how to give them to Rack::Session::Memcache

Edit: or to Rack::Session::Dalli that would be great too https://github.com/mperham/dalli


Solution

  • This config worked for Heroku, Dalli is clever and knows to look in the environment variables

        require 'dalli'
        require 'rack/session/dalli'
        use Rack::Session::Dalli, :cache => Dalli::Client.new
    

    After reading the source code at https://github.com/mperham/dalli/commit/4ac5a99