breaking into the unknown…


memcached session store for devise in rails 3

session can be stored in cookies on client side or in database or memcached or any other store on server side. By default, rails store the session in cookies store. You can configure, your rails app to use any other store. I will explain it to configure with memcached.

memcached is a cache server, which can cache your resources. It can have maximum size of 64 MB, good enough to provide a excellent cache feature to your application. Some good insight on its benefits and pitfall is explained here .

NOTE : you do not need to do any thing special to change devise session store other then telling about the new cache store. It is smart enough to work with any cache store your rails app is configured to work with.

So, here we will configure our rails app to use memcached as cache store rather then the default cookies store.

STEP 1: Installing memcached

memcached is supported by all linux server ubuntu, redhat,centos etc. I am using ubuntu machine as my server, if you are using any other machine , change the command accordingly.

$ sudo apt-get install memcached

$ sudo /etc/init.d/memcached start # strat the memcahed server, the other options are {start|stop|restart|force-reload|status}

STEP 2: Installing the gem to handle memcached

A no of gems available there acting as memcached client, we will use the most populer one : dalli gem

Add below to gemfile and run bundle install

gem ‘dalli’

STEP 3: configure the cache store in environment file

Add below line to the environment file, in which you want to use memcahed, say I added it to staging.rb and production.rb. Be sure that, in whatever environment you have set mecached store, on that server memcached is installed and running, otherwise your session will not work.

config.cache_store = :dalli_store # It will manage cache for us with memcached

STEP 4: configuring devise to use the new cache store

We need to tell, devise, where to store its session data. With rails 3 the session configuration is present in

config/initializers/session_store.rb   file. You can see below line there

MyFinance::Application.config.session_store :cookie_store, key: ‘_my_finance_session’ # my_finance is my project name, the key can have any random string, but what you are seeing is generated by devise by default when it is installed.

Replace, it with the new configuration as below

MyFinance::Application.config.session_store ActionDispatch::Session::CacheStore, :expire_after => 30.minutes

This line will set devise session store to what ever cache_store set in the environment file

This, is the simplest configuration, where session is set to expire in 30 minute, there is a number of other option you can pass.You can get complete list of option here

That’s all, now whenever a user session is validated, you can see line like below on console

Cache read: _session_id:7ef18e9eae1e2d595d405820bf5b0b5d

This show that, your session is now not read from cookies but the memcached…so things working as expected 🙂