# Cached models

**URL:** <https://www.ruby-forum.com/t/cached-models/132983>\
**Category:** Rails\
**Created:** [March 26, 2008, 1:27pm UTC](https://www.ruby-forum.com/t/cached-models/132983 "2008-03-26T13:27:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 26, 2008, 1:27pm UTC](https://www.ruby-forum.com/t/cached-models/132983/1 "2008-03-26T13:27:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 26, 2008, 2:49pm UTC](https://www.ruby-forum.com/t/cached-models/132983/2 "2008-03-26T14:49:52Z")

</div>

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

> end
> 
> @facilities = Facility.all\_facilities

* * *

thanks - made it simple…I likee

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 26, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/cached-models/132983/3 "2008-03-26T14:43:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 26, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/cached-models/132983/4 "2008-03-26T15:15:39Z")

</div>

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.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
```
  
--~--~---------~--~----~------------~-------~--~----~  
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 removed\_email\_address@domain.invalid   
 To unsubscribe from this group, send email to removed\_email\_address@domain.invalid   
 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en   
 -~----------~----~----~----~------~----~------~--~---

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 26, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/cached-models/132983/5 "2008-03-26T15:51:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 26, 2008, 4:38pm UTC](https://www.ruby-forum.com/t/cached-models/132983/6 "2008-03-26T16:38:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 26, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/cached-models/132983/7 "2008-03-26T16:35:16Z")

</div>

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.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> ```
>   
> --~--~---------~--~----~------------~-------~--~----~  
> 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 removed\_email\_address@domain.invalid   
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid   
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en   
> -~----------~----~----~----~------~----~------~--~---

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 26, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/cached-models/132983/8 "2008-03-26T16:52:25Z")

</div>

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
