Question about sessions and code re-use


#1

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


#2

On 1/16/07, nephish removed_email_address@domain.invalid 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 W.
http://errtheblog.com


#3

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


#4

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


#5

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