First install memcached in your machine and make sure its running.

After installing memcached to make sure its running, try starting the server using the below command in the terminal

/etc/init.d/memcached start 

memcached -vv # show memcached log in console

add the below to your Gemfile

gem ‘memcache-client’ # memcached client for ruby <!–more–>

In your application.rb file add the below code.I am using cofigatron gem to set the value for host and port.

Configatron makes configuring your applications and scripts incredibly easy. No longer need to use constants or global variables.

configatron.memcached.host = “localhost”

configatron.memcached.port = 11211

config.after_initialize do

    config.cache_store = :mem_cache_store, “#{configatron.memcached.host}:#         {configatron.memcached.port}”, {:namespace => “memcache_app”}      config.action_controller.cache_store = :mem_cache_store

end

 Thats it all done, now start your memcahced server and play around with caching.

 

Advertisements