Cached models

I seem to have @facilities = Facility.find(:all) in a lot of places in
my application and my facilities model data rarely changes. How can I
cache it so that I don’t have to hit the DB each time?

Craig

On Wed, 2008-03-26 at 14:43 +0100, Mark B. wrote:

end

@facilities = Facility.all_facilities


thanks - made it simple…I likee

Craig

Craig W. wrote:

I seem to have @facilities = Facility.find(:all) in a lot of places in
my application and my facilities model data rarely changes. How can I
cache it so that I don’t have to hit the DB each time?

class Facility < ActiveRecord::Base
after_save {@@all_facilities = nil}
def self.all_facilities
@@all_facilities ||= self.find(:all)
end
end

@facilities = Facility.all_facilities

Craig W. wrote:
On Wed, 2008-03-26 at 14:43 +0100, Mark B. wrote:
  
Craig W. wrote:
    
I seem to have @facilities = Facility.find(:all) in a 
lot of places in
my application and my facilities model data rarely changes. How can I
cache it so that I don't have to hit the DB each time?
      
class Facility < ActiveRecord::Base
  after_save {@@all_facilities = nil}
  def self.all_facilities
    @@all_facilities ||= self.find(:all)
  end
end

@facilities = Facility.all_facilities

----
thanks - made it simple...I likee

Craig

Note this will only work if you are only running a single process.

--
Jack C.
[email protected]

--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

On Wed, 2008-03-26 at 09:17 -0500, Jack C. wrote:

after_save {@@all_facilities = nil}

Craig

Note this will only work if you are only running a single process.


ruh-roh…

I run several mongrels…no workee? Or will each cache separately?

Craig

Craig W. wrote:

I run several mongrels…no workee? Or will each cache separately?

Each will cache separately but what Jack means is that if one process
makes a change, the others will not notice and they will have stale
data.

In that situation, you can cache in an external repository (such as
memcache) which each thread is able to update.

Craig W. wrote:
On Wed, 2008-03-26 at 09:17 -0500, Jack C. 
wrote:
  
Craig W. wrote:
    
On Wed, 2008-03-26 at 14:43 +0100, Mark B. wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Craig W. wrote:

    </pre>
    <blockquote type="cite">
      <pre wrap="">I seem to have @facilities = Facility.find(:all) 

in a lot of places in
my application and my facilities model data rarely changes. How can I
cache it so that I don’t have to hit the DB each time?

      </pre>
    </blockquote>
    <pre wrap="">class Facility &lt; ActiveRecord::Base

after_save {@@all_facilities = nil}
def self.all_facilities
@@all_facilities ||= self.find(:all)
end
end

@facilities = Facility.all_facilities

    </pre>
  </blockquote>
  <pre wrap="">----

thanks - made it simple…I likee

Craig

  </pre>
</blockquote>
<pre wrap="">Note this will only work if you are only running a 

single process.

----
ruh-roh...

I run several mongrels…no workee? Or will each cache separately?

Each process will have its own cache.
Craig
  


--
Jack C.
[email protected]

--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

On Wed, 2008-03-26 at 16:38 +0100, Mark B. wrote:

Craig W. wrote:

I run several mongrels…no workee? Or will each cache separately?

Each will cache separately but what Jack means is that if one process
makes a change, the others will not notice and they will have stale
data.

In that situation, you can cache in an external repository (such as
memcache) which each thread is able to update.


cool…I’m install memcached now (Linux) and simulataneously making sure
that it’s available for my production system.

That seems to be a better way to go

Thanks

Craig