Topic: Is this an efficient way to use before_filter in application.rb?

I'm trying to find ways to make my application more efficient.  Here, I have a before_filter in the application.rb, which sets vital information for the application.  However, as I'm looking at the log, it seems that these calls are made everytime I go to a page or do anything.  Is there a way to make these more efficient?

# in application.rb
before_filter :set_vital_info

def set_vital_info
  @current_account = Account.find(session[:id]) if @current_account.nil? && session[:id]   
  unless session[:app_current_year]   
    session[:app_current_year] = Configuration.find_by_category('current_year').value
  end
  @current_year = session[:app_current_year]
  @current_version = Configuration.find_by_category('version').value if @current_version.nil?
  @app_email = Configuration.find_by_category('app_email_address').value if @app_email.nil?
  @msg_fade_time= Configuration.find_by_category('msg_fade_time').value if @msg_fade_time.nil?
end

The only alternative I could think of would be storing them in sessions, but I really don't know if that's faster or not.  Anyone know of a more efficient way to do this?  Thanks.

Re: Is this an efficient way to use before_filter in application.rb?

daniel_l wrote:

I'm trying to find ways to make my application more efficient.  Here, I have a before_filter in the application.rb, which sets vital information for the application.  However, as I'm looking at the log, it seems that these calls are made everytime I go to a page or do anything.  Is there a way to make these more efficient?

...

The only alternative I could think of would be storing them in sessions, but I really don't know if that's faster or not.  Anyone know of a more efficient way to do this?  Thanks.

Storing things in the session adds overhead, So to reduce the stress of the overhead its nice to move the session into something thats a bit quicker.

If your storing the session in the cookie then it gets pretty large to send back and forth between the user and the server, and for secure information you should be careful.

If you store it in a memcache you can help boost the speed of the application because it won't have to hit the DB again to get the data, it can pull it from the memcache instead.

You can also store it in the db, and just have one db call to retrieve it.

Memcache is considered one of the faster solutions.

Another option is to start caching DB calls, which you can also store in the memcache with the sessions. That will help speed up the calls if there are lots of repeated DB calls, like on every page load.

You can also store things in a DRb (Distributed Ruby process)  which acts similar, functionally, to how memcache works. DRb will store things in the memory of another ruby process, but its not as versatile as a memcache is.

http://www.railsenvy.com/2007/2/28/rail … g-tutorial

Is pretty good to understand caches and caching solutions.