Configuring Memcached with Rails 3

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.
[source=’ruby’]
/etc/init.d/memcached start #It may show memcached already, if started.
memcached -vv # show memcached log in console
[/source]

add the below line to your .Gemfile file.
[source=’ruby’]
gem ‘memcache-client’ # memcahced ruby client
[/source]
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’]
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
[/source]
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 *