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

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

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

Craig White wrote:

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

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

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

Craig White wrote:
>
> > 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
> >
> ----
> thanks - made it simple...I likee
>
> Craig
>
>
Note this will only work if you are only running a single process.

Craig White wrote:

Craig White wrote:
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

----
thanks - made it simple...I likee
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?

Each process will have its own cache.

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.

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.