Question

I am not able to use memcached as session store with rails 4 using dalli gem.

Here's what I have done.

I added dalli gem to Gemfile

gem 'dalli'

I added the following line in config/initializers/session_store.rb

Rails.application.config.session_store ActionDispatch::Session::CacheStore, :expire_after => 20.minutes

And I added the following line in development.rb

config.cache_store = :dalli_store

Now when I start my development server with thin server without starting memcached server, I still can login as usual. Should I get some error like no memcached server running or something like that.

I am not sure if rails is using memcached as session store or not.

Can someone tell me what have I missed in using memcached as session store in development environment?

For your information, I have been using devise as authentication gem.

Thanks

Was it helpful?

Solution

Yes, you should see an error like this in the console:

DalliError: No server available

However, you will still get the session cookie, since Rails will generate it and send it to the browser. it's just that Rails does not have a place to store the data associated with the cookie.

So, for a demo, try this:

  1. Stop memcached. In some controller action do this:

    def some_action
      puts session[:test]
      session[:test] = "hello"
    end  
    

    You should not see "hello" in STDOUT.

  2. Now, restart memcached and hit the action again. (might need to refresh the browser twice). This time, you should see "hello".

  3. If you again stop memcached, the "hello" will no longer be displayed.

I hope that makes it clear that the generation of the cookie (containing the session key) and the storage of data against the value of the cookie (i.e. the session key) are two different things. And of course, ensure that memcached really is stopped.

As for the part being able to login even with memcached stopped, check to see that you have cleared all cookies for the domain (localhost) and that you have restarted the rails server after making the change. Also, clear out the tmp/cache directory.

PS. If you do not see the error DalliError: No server available then that means that memcached is probably still running somewhere. Try accessing memcached via Dalli via the Rails console and see if you are able to store/get data.

PPS. If you see files being stored in tmp (like tmp/cache/D83/760/_session_id%3A4d65e5827354d0e1e8153a4664b3caa1), then that means that Rails is falling back to FileStore for storing the session data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top