Friday, January 13, 2012

Scalability in Rails - Exploring Caching

Many of us when talk about Rails, are concerned with scalability of Rails applications.

I got a chance to improve scalability of a particular module in my application through Rails Caching.

Here's how it works in Rails 3.x series:

Rails, basically comes with a inbuilt Caching mechanism which is by default enabled in  production mode.
In each of the Modes of deploying a Rails app, i.e. Dev, Test, Prod we need to make sure caching is enabled in the respective app/environments/mode.rb file


The command which will set Caching to true in these environments is:

config.action_controller.perform_caching = true

Now , Say if you are implementing Rails -> Action Caching, In your controller You need to enter:-

caches_action :index, :layout => false

def index
# your code goes in here

This means, Rails is going to cache the Index action for you. Of course , with Caching, we need to also consider Expiry of Cache.

What I gotta implement is not a Batch Process which regularly checks for Cache Expiry, but a more efficient approach which is within my controller itself. This way I am saving myself and others who use this code in the future from the overhead of managing and monitoring a batch process time and again.

This is how the Cache Expiry action can look:-

def clear_cache
t = Rails.cache.fetch('variable_used_to_check_expiry_time') ||
t = Time.parse(t) if t.is_a?(String)
expire_action :action => :index if > t

Now, how do I call for clear_cache to check when it is actually time to clear the cache..? Well, this can be done efficiently , automatically using a before_filter method.. Like this..
before_filter => :clear_cache

Rails Cache, comes with a <b>Cache log</b> on which we can specify when the a page was first accessed as a result of the particular Rails action being called.

Now for e.g. if I access the index.html.erb at 20:29 pm, this gets stored in my Rails cache log through the following way:-

def index
# your code goes in here
Rails.cache.write('variable_used_to_check_expiry_time', + 6.hours)
end I'm setting my cache expiry time to current time  + 6 hrs..i.e. at 04:29am,

Now only if the page => index.html.erb if accessed after 04:29am the following day, the new cache expiry time can be set....

You might be wondering what is written into cache log when the controller method of clear cache is actually called for the first time? Thats a good question..:)

Well basically what happens is, the current time is set into var t, since t = current time, no clear cache action needs to be executed to worry us.

P.S:- Credits to Srikanth Jeeva for inputs on the clear cache approach..

Overcoming RVM Command Not Found Error in Ubuntu 11.04 Server


I faced this weird issue of rvm command not being found, inspite of installing it quite a few times.

Please find details wrt the issue:-

I am trying to set up RVM On Ubuntu 11.04 Server.

Making use of a multi user install..

The error I am currently getting is:-

    $  sudo bash -s stable < <(curl -s )
    -sh: Syntax error: redirection unexpected

I am not sure whats wrong with the command, as I am using an exact copy of the command from the [RVM installation guide]

Any inputs, could be really helpful..

When I login as root, I am able to atleast run the command and install rvm. But the rvm gives me a command not found.

    root@host:/home/s244574# sudo bash -s stable < <(curl -s )
    Downloading RVM from wayneeseguin branch stable
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  796k  100  796k    0     0   139k      0  0:00:05  0:00:05 --:--:--  261k
    Upgrading the RVM installation in /root/.rvm/
    installing - /root/.rvm/man/man1/rvm.1.gz -
    installing - /root/.rvm/man/man1/rvm.1 -
        RVM system user group 'rvm' exists, proceeding with installation.
    Upgrade Notes:
      * No new notes to display.
    # RVM:  Shell scripts enabling management of multiple ruby environments.
    # RTFM:
    # HELP: (#rvm on
    # Screencast:
    # In case of any issues read output of 'rvm requirements' and/or 'rvm notes'
    Upgrade of RVM in /root/.rvm/ is complete.
    # root,
    #   Thank you for using RVM!
    #   I sincerely hope that RVM helps to make your life easier and more enjoyable!!!
    # ~Wayne
    root@host:/home/s244574# rvm
    No command 'rvm' found, but there are 19 similar ones
    rvm: command not found

Few points to note, wrt what I tried:-

Initially, rvm was installed for me in the path:-


but somehow rvm command doesn't seem to recognize the path where it is insalled, if I am guessing correctly.

I also tried [install if root only] , the installation path changes(to /root/.rvm/ ) but still I face the same issue.

I have a user, with loginname "user1" is there a way, I can let the user1 identify the rvm path, which has the post installation files, so that the command can be recognized

Thank you..


The Fix which did the trick for me:-

source ~/.bash_profile