Subhashmohan's Blog

Archive for March 2010

Installation

1. Follow this link to download and install memcached.
2. Start the memcached server using either

memcached -vv

or

/etc/init.d/memcached start

Integrating memcached with your rails application.

1. Install memcache-client

gem install memcache-client

2. Add the below line to  end of your config/environment.rb file.

CACHE = MemCache.new('127.0.0.1')

3. Add the following method to your application controller / application helper or where ever you want according to your need.

def perform_cache(key)
  begin
   unless output = CACHE.get(key)
     output = yield
     CACHE.set(key, output, 15.minutes)
   end
 rescue => e
  logger.info “MEMCACHE ERROR++++++++++++++++++++++++++++++++++++”
  logger.info “ERROR : #{e.message}”
  logger.info “MEMCACHE ERROR++++++++++++++++++++++++++++++++++++”
  output = yield
  end
  return output
 end

The above method performs the caching based on a unique key. If the value for that key already exists, it will return the output from the stored cache or  else it will execute the block you are using when calling the above method and generate a new value from the executed block for the particular key and store it in the cache.

You can set a timer to expire the cached data.(15 mins in my case.)

If you don’t want to expire the cache based on timer, simply remove the third parameter we are passing to ’set’ method.

The above method also handles the exception,in case your memcache server is down for some reason.

4.  Cache your complex queries/methods as follows.

my_key =  Digest::MD5.hexdigest(request.request_uri)
output = perform_cache(my_key) { ClassName.method_name(…) }

Note : The key generated should be always unique, otherwise it will overwrite your existing cached data already having the same key. I am using the URL as the key, Since the URL is pretty long we can take the MD5 hash/SHA1 hash of it and use it as the key.

5.  You can edit memory  used by memcached as follows.

Open /etc/sysconfig/memcached file and edit “CACHESIZE” entry,
this entry is in MBs.

That's it your ready to go.

Introduction

gem “whenever“  provides you with the valid cronjob syntax and also writes / updates the crontab file for you.

Installation

1.  Install the gem

gem install whenever

2. Add below line to your config/environment.rb

config.gem ‘whenever’, :lib=>false, :source=>’http://gemcutter.org’

3.  Go to your application root directory and run the below command

wheneverize .

   This will create a “config/schedule.rb” file for you.

  EXAMPLE “config/schedule.rb”

  set :environment, :development

  set :path, "/var/www/MyApp"

  # Scheduled Hourly

   every 4.hours do
   command "/usr/bin/your_command"
   end

  # Scheduled Daily

   every 1.day, :at => '12:00 am' do
   runner "MyModel.yourmethod"
   end

   # Scheduled Weekly

   every :monday, :at => '11:00 pm' do
   rake   "your:rake:task"
   end

Consider the above as the contents of your config/schedule.rb file.

4.  Now from application root directory, run the below command

whenever

The above command will output valid syntax for the crontab as below.

 0 0 * * * /var/www/MyApp/script/runner -e development “Model.your_method”

 0 0,4,8,12,16,20 * * * /usr/bin/your_command

 0 23 * * 1 cd /var/www/MyApp && RAILS_ENV=development /usr/bin/env rake your:rake_task
 

5. Now, To write the above content in your Crontab file run the below command.

whenever –update-crontab

you can see the above entries in your crontab file by running “crontab -e”.

That’s it, you are ready to go.

 





Blog Stats

  • 4,305 hits
March 2010
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  

Categories

Pages