# Object caching

**URL:** <https://www.ruby-forum.com/t/object-caching/112212>\
**Category:** Rails\
**Created:** [August 16, 2007, 12:52pm UTC](https://www.ruby-forum.com/t/object-caching/112212 "2007-08-16T12:52:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [August 16, 2007, 12:52pm UTC](https://www.ruby-forum.com/t/object-caching/112212/1 "2007-08-16T12:52:46Z")

</div>

Does Rails cache all objects that are accessed during a request?

If one object is accessed several times during a request, would its  
value be read from memory, or fetched from the database over and over  
again?

I’m sure that objects that were passed by the controller, like  
@something would be cached, because their reference is passed, but  
what about other objects which might be deleted by the garbage  
collection?

So, if I plan to display the same object over and over again within a  
partial, for the same call, how do I make sure it’s not fetched from  
the database on every render?

Thanks,  
Amir

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [August 16, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/object-caching/112212/2 "2007-08-16T15:58:13Z")

</div>

You can pre-fetch the data in the controller. If it is an associated  
model, you can use find()'s :include to fetch it in one sql call.

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000992)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

helzer wrote:

> So, if I plan to display the same object over and over again within a  
> partial, for the same call, how do I make sure it’s not fetched from  
> the database on every render?
> 
> Thanks,  
> Amir
> 
> >

–  
Sincerely,

William P.

[http://www.billpratt.net](http://www.billpratt.net)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [August 18, 2007, 12:13am UTC](https://www.ruby-forum.com/t/object-caching/112212/3 "2007-08-18T00:13:51Z")

</div>

Hi,

> model, you can use find()'s :include to fetch it in one sql call.  
> [ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000992)
> 
> > Does Rails cache all objects that are accessed during a request  
> > Also… ActiveRecord will keep a pretty simple (but somehow effective)  
> > cache on a per-model basis. The last query result for each model is  
> > cached, and if you are trying to execute the same query, then it will be  
> > coming directly from memory. Take into account there is only a cached  
> > resultset per model.

You can play a bit with that to see how it works by opening a console in  
development mode and executing some finds as you are tailing the log.  
You’ll see when you repeat the same find against a model, no new query  
is launched againts the db.

If I’m not mistaken, rails 2 will improve the AR cache, but so far  
that’s the way it works.

regards,

javier ramirez

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [August 18, 2007, 12:56am UTC](https://www.ruby-forum.com/t/object-caching/112212/4 "2007-08-18T00:56:55Z")

</div>

Hi again,

Sorry about that, but my prior email was completely messed up. I really  
don’t know how i got the idea of a per-model last query cache (maybe an  
old rails version? not sure of that), but truth is AR doesn’t cache like  
that.

There is a cache for the associations, so if you have a variable with a,  
for example, has\_many and you call on the association, only the first  
time you use it is the query to be launched.

Sorry once again about the misleading,

javier

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [August 22, 2007, 4:18pm UTC](https://www.ruby-forum.com/t/object-caching/112212/5 "2007-08-22T16:18:27Z")

</div>

Hi Rick,

Your plugin is great. So, I guess when Rails 2 comes out, I should  
probably stop using it, as it’s already integrated, right?

Thanks,  
Amir

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [August 18, 2007, 1:38am UTC](https://www.ruby-forum.com/t/object-caching/112212/6 "2007-08-18T01:38:59Z")

</div>

On 8/17/07, javier ramirez [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> time you use it is the query to be launched.
> 
> Sorry once again about the misleading,

Actually, Active Record on edge rails does some caching for DB  
queries. It’s nothing sophisticated, but it helps out a lot. I wrote  
an article comparing the different approaches in AR that you can use  
(piggy backing, eager includes, query caching, and an object caching  
approach in a plugin I wrote)

[http://activereload.net/2007/5/23/spend-less-time-in-the-database-and-more-time-outdoors](http://activereload.net/2007/5/23/spend-less-time-in-the-database-and-more-time-outdoors)

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
