# Storing Tables in Memory

**URL:** <https://www.ruby-forum.com/t/storing-tables-in-memory/80505>\
**Category:** Rails\
**Created:** [December 27, 2006, 11:15pm UTC](https://www.ruby-forum.com/t/storing-tables-in-memory/80505 "2006-12-27T23:15:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 27, 2006, 11:15pm UTC](https://www.ruby-forum.com/t/storing-tables-in-memory/80505/1 "2006-12-27T23:15:59Z")

</div>

Is there a was to specify to Rails that it should hold the contents of  
a table in memory?

For instances, I have a lookup table that is a two column table (id and  
description) and then lots of things that belong\_to those its.

class A \< ActiveRecord::Base

belongs\_to :lookup

end

I’d love to able to say a.lookup.description without needing another  
SQL call. I know you can avoid the call using the :include feature, but  
that means joins in the SQL backend, and I’m doing LOTS of these  
queries.

Something like this would be great:

class Lookup \< ActiveRecord::Base

:cache\_table\_in\_memory

end

Then all operations on the Lookup class would be done in memory. It  
would also add a refresh method that you could manually call on the  
infrequent event where the lookup table changes.

If something like this exists, can someone point me in the right  
direction? If not, consider it a feature suggestion.

Thanks,

Andrew

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 28, 2006, 10:49am UTC](https://www.ruby-forum.com/t/storing-tables-in-memory/80505/2 "2006-12-28T10:49:25Z")

</div>

Andrew S. wrote:

> If something like this exists, can someone point me in the right  
> direction? If not, consider it a feature suggestion.
> 
> Thanks,
> 
> Andrew  
> I hope you are aware of cached\_model plugin on top of memcached by  
> honourable drbrain aka Eric H. of Holy School of Seattle.rb.

Now, of course, you can’t use the above plugin, if you are not running  
memcached. Still, quite a few DRb based caching mechanisms are sprouting  
all over the place.

You can use, Bakcgroundrb to cache values or else you can use this:  
[http://boogaloo.rubyforge.org/doc/](http://boogaloo.rubyforge.org/doc/)

Or if you are unhappy with all the above shit, roll your own and have  
your name embodied in golden chapters of rails development forever.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 28, 2006, 11:57am UTC](https://www.ruby-forum.com/t/storing-tables-in-memory/80505/3 "2006-12-28T11:57:36Z")

</div>

Have a look at Trevor S. enumerations mixin which allows you to  
treat instances of your ActiveRecord models as though they were an  
enumeration of values while caching Instances in memory.

[http://svn.protocool.com/rails/plugins/enumerations\_mixin/trunk/](http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk/)

On 12/28/06, Hemant K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > infrequent event where the lookup table changes.  
> > Now, of course, you can’t use the above plugin, if you are not running
> 
> >

–  
Never be afraid to try something new. Remember, amateurs built the  
ark; professionals built the Titanic!
