Configuring Memcached with Rails 3

Surendran Sukumaran

34 sec read

First install memcached in your machine and make sure the memcached server is running.
After installation to make sure its running, try starting the server using the below command.
/etc/init.d/memcached start #It may show memcached already, if started.
memcached -vv # show memcached log in console

add the below line to your .Gemfile file.
gem ‘memcache-client’ # memcahced ruby client
In your application.rb file add the below code.
I am using configatron gem to set the value for host and port.
Configatron makes configuring your applications and scripts incredibly easy. With configatron, you no longer need to use constants or global variables.
[source=’ruby’] = “localhost”
configatron.memcached.port = 11211
config.after_initialize do
config.cache_store = :mem_cache_store, “#{}:#{configatron.memcached.port}”, {:namespace => “memcache_app”}
config.action_controller.cache_store = :mem_cache_store
Thats it. Now you can start using memcached in your Rails3 App.

Related posts:

2 Replies to “Configuring Memcached with Rails 3”

  1. Hi ,
    Thanks for your blog.
    Can we validate request input data using memcache ? Actually I am facing scenario in which user uploading CSV file which can have hundreds of rows and to validate each row using ActiveRecord taking long time.
    Please can you reply me below question:-
    1. So can we cache our validation rule in memcache ?
    2. If it is possible to store validation rules in memcache but can I able to perform
    validation check ?

  2. Hi Fahim,
    Storing validation rules in memcache will not help you.
    memcache is used for storing data. May be you can use that memcache stored dataset to validate your csv file rowset.
    Consider using some parsing gems to improve performance.

Leave a Reply

Your email address will not be published. Required fields are marked *