Ruby Forum Ruby on Rails > cached models

Posted by Craig White (Guest)
on 26.03.2008 13:27
(Received via mailing list)
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
Posted by Mark Bush (bush)
on 26.03.2008 14:43
Craig White 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
Posted by Craig White (Guest)
on 26.03.2008 14:49
(Received via mailing list)
On Wed, 2008-03-26 at 14:43 +0100, Mark Bush wrote:
> end
> 
> 
> @facilities = Facility.all_facilities
----
thanks - made it simple...I likee

Craig
Posted by Jack Christensen (Guest)
on 26.03.2008 15:15
(Received via mailing list)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" 
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Craig White wrote:
<blockquote
 cite="mid:1206539334.25212.33.camel@lin-workstation.azapple.com"
 type="cite">
  <pre wrap="">On Wed, 2008-03-26 at 14:43 +0100, Mark Bush wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Craig White 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>
Note this will only work if you are only running a single process.<br>
<br>
<pre class="moz-signature" cols="72">--
Jack Christensen
<a class="moz-txt-link-abbreviated" 
href="mailto:jackc@hylesanderson.edu">jackc@hylesanderson.edu</a></pre>
<br>
--~--~---------~--~----~------------~-------~--~----~<br>
You received this message because you are subscribed to the Google 
Groups &quot;Ruby on Rails: Talk&quot; group. <br> To post to this 
group, send email to rubyonrails-talk@googlegroups.com <br> To 
unsubscribe from this group, send email to 
rubyonrails-talk-unsubscribe@googlegroups.com <br> For more options, 
visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en <br> 
-~----------~----~----~----~------~----~------~--~---<br>
</body>
</html>
<br>
Posted by Craig White (Guest)
on 26.03.2008 15:51
(Received via mailing list)
On Wed, 2008-03-26 at 09:17 -0500, Jack Christensen 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
Posted by Jack Christensen (Guest)
on 26.03.2008 16:35
(Received via mailing list)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" 
http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Craig White wrote:
<blockquote
 cite="mid:1206543073.25212.37.camel@lin-workstation.azapple.com"
 type="cite">
  <pre wrap="">On Wed, 2008-03-26 at 09:17 -0500, Jack Christensen 
wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Craig White wrote:
    </pre>
    <blockquote type="cite">
      <pre wrap="">On Wed, 2008-03-26 at 14:43 +0100, Mark Bush wrote:

      </pre>
      <blockquote type="cite">
        <pre wrap="">Craig White 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.
    </pre>
  </blockquote>
  <pre wrap=""><!---->----
ruh-roh...

I run several mongrels...no workee? Or will each cache separately?
  </pre>
</blockquote>
Each process will have its own cache.<br>
<blockquote
 cite="mid:1206543073.25212.37.camel@lin-workstation.azapple.com"
 type="cite">
  <pre wrap="">
Craig




  </pre>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">--
Jack Christensen
<a class="moz-txt-link-abbreviated" 
href="mailto:jackc@hylesanderson.edu">jackc@hylesanderson.edu</a></pre>
<br>
--~--~---------~--~----~------------~-------~--~----~<br>
You received this message because you are subscribed to the Google 
Groups &quot;Ruby on Rails: Talk&quot; group. <br> To post to this 
group, send email to rubyonrails-talk@googlegroups.com <br> To 
unsubscribe from this group, send email to 
rubyonrails-talk-unsubscribe@googlegroups.com <br> For more options, 
visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en <br> 
-~----------~----~----~----~------~----~------~--~---<br>
</body>
</html>
<br>
Posted by Mark Bush (bush)
on 26.03.2008 16:38
Craig White 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.
Posted by Craig White (Guest)
on 26.03.2008 16:52
(Received via mailing list)
On Wed, 2008-03-26 at 16:38 +0100, Mark Bush wrote:
> Craig White 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