Forum: Ruby on Rails question about sessions and code re-use

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2007-01-16 22:23
(Received via mailing list)
hey there,

i have a simple model called key_words.rb
the purpose of this is to hold system config data.
it also holds the parameters that our system uses to judge the
condition of the stuff we monitor.

anyway, one of the methods is this

def self.get_in_wet_list(status)
        wet_list = find(:first,
                        :conditions => "name = 'wet_list' ")
        wet_list.message.split(',').include?(status)

    end


my question is.... we use this to process a lot of information,
sometimes a few thousand records
will go thru this and i think it could be a performance killer.
i wanted to make this a session variable so that it would not have to
go to the database and redraw it a few thousand times on a page
refresh.

so i thought
def self.get_in_wet_list(status)
        wet_list = session[:wet_list]
        if wet_list.nil?
            wet_list = find(:first,
                        :conditions => "name = 'wet_list' ")
        end
        wet_list.message.split(',').include?(status)
end


the only thing is that a model does not seem to be able to access
session variables.

at any rate, does someone have an idea about how to make this work
better ?

thanks
B8dbb1987e8e5318584865f880036796?d=identicon&s=25 Chris Wanstrath (defunkt)
on 2007-01-17 11:26
(Received via mailing list)
On 1/16/07, nephish <nephish@gmail.com> wrote:

> at any rate, does someone have an idea about how to make this work
> better ?

What you're looking for is caching.  Check out BackgrounDRb or
something similiar.

If your wet_list is fairly static and not too massive you could cache
it in a class variable, too:

class WetList < ActiveRecord::Base
  @@wet_list_cache = nil

  def self.get_in_wet_list(status)
    @@wet_list_cache ||= find_by_name('wet_list')
    @@wet_list_cache.message.split(',').include? status
  end

  def after_save
    @@wet_list_cache = nil
  end
end

--
Chris Wanstrath
http://errtheblog.com
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2007-01-17 15:18
(Received via mailing list)
thanks a whole lot !
yes, the variable is fairly static, changes maybe once a week or month.
is small too, and example would be wet_list = 'w,W,ww,rw' and thats it.
thanks so much for this help !

sk
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2007-01-17 15:54
(Received via mailing list)
ok, one more thing,
i have a wet_list, but also have a dry_list , fwd_list, etc...
i suppose i could lump them all into one class and each list could be
another method.
so does rails keep up with this model for the whole session ?

thanks !
shawn
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2007-01-17 18:40
(Received via mailing list)
forget it, i went ahead and tried it and it worked like a charm,
pages are loading much much faster.
thanks for taking your time on this for me, Chris.
You really made a difference
This topic is locked and can not be replied to.