On 7 Mar 2008, at 09:47, Giant Cranes wrote:
>
> 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?
>
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
> 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
> --
> Posted via http://www.ruby-forum.com/.
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---