Hi,
I have a number of models which contain almost static data. I don’t want
to hit to db when accessing a row, nor do I want to hit memcached.
I am hoping to use a lazy-loaded model class variable hash. This will
ensure that the db is hit only once for the life of a mongrel process.
Are there any drawbacks to this solution? Can the code be simplified?
class Status < Activerecord:Base
@@hash_by_name = nil
def self.hash_by_name
unless @@hash_by_name
@@hash_by_name = Hash.new
Status.find(:all).each { |status| @@hash_by_name[status.name] =
status }
end
@@hash_by_name
end
end
#Allowing me to do something like this:
puts Status.hash_by_name[‘pending’].id
Thanks,
GiantCranes
On 7 Mar 2008, at 09:47, Giant C. wrote:
Are there any drawbacks to this solution? Can the code be simplified?
well it’s very minor, but you can write
@@hash_by_name = Status.find(:all).index_by(&:name)
I’ve used a similar pattern quite a lot and as far as I can tell it
works fine. If you are going to be using it in a lot of methods, then
you can wrap it up a little, i’ve got something like
module Caches
module ClassMethod
def caches(name, &block)
define_method(name) do
ivar_name = “cache_#{name}_value”
unless value = instance_variable_get ivar_name
value = instance_eval(&block)
instance_variable_set ivar_name, value
end
value
end
end
end
def self.included(base)
base.extend ClassMethods
end
end
(written off the top of my head in mail, expect typos).
You can then do something like
class Status …
class << self
include Caches
caches(‘all’) {Status.find(:all).index_by(&:name)}
caches(‘something_else’) { …code to compute something else…}
end
end
Fred
Thanks Frederick, with your help I’ve reduced the code to:
class Status < ActiveRecord::Base
def self.hash_by_name
@@hash_by_name ||= self.find(:all).index_by(&:name)
end
end
I’ll use the helper method if I use this a lot in the future.
Thanks!