# Caching data in memory

**URL:** <https://www.ruby-forum.com/t/caching-data-in-memory/152743>\
**Category:** Rails\
**Created:** [November 7, 2008, 12:28am UTC](https://www.ruby-forum.com/t/caching-data-in-memory/152743 "2008-11-07T00:28:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thabenksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thabenksta](https://www.ruby-forum.com/u/thabenksta)\
**Post date:** [November 7, 2008, 12:28am UTC](https://www.ruby-forum.com/t/caching-data-in-memory/152743/1 "2008-11-07T00:28:10Z")

</div>

I need to load some read-only lookup data from the database into memory  
for fast access (i.e. caching). What’s the best way to cache data in  
memory in Rails apps? For example, should I use global, class or  
instance variables? I realize each controller in my cluster will have  
it’s own copy, which is fine.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![thabenksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thabenksta](https://www.ruby-forum.com/u/thabenksta)\
**Post date:** [November 7, 2008, 10:31am UTC](https://www.ruby-forum.com/t/caching-data-in-memory/152743/2 "2008-11-07T10:31:17Z")

</div>

On 6 Nov 2008, at 23:28, Ben K. wrote:

> I need to load some read-only lookup data from the database into  
> memory  
> for fast access (i.e. caching). What’s the best way to cache data in  
> memory in Rails apps? For example, should I use global, class or  
> instance variables? I realize each controller in my cluster will have  
> it’s own copy, which is fine.

Doesn’t need to be anything cleverer than a class variable (or a class  
instance variable).

Fred

---

<div class="post-metadata">

**Author:** ![thabenksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thabenksta](https://www.ruby-forum.com/u/thabenksta)\
**Post date:** [November 7, 2008, 4:00pm UTC](https://www.ruby-forum.com/t/caching-data-in-memory/152743/3 "2008-11-07T16:00:49Z")

</div>

Thanks, Fred.

Has anyone used Rails.cache.write / read? I wonder how good of a  
solution that is.

---

<div class="post-metadata">

**Author:** ![thabenksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thabenksta](https://www.ruby-forum.com/u/thabenksta)\
**Post date:** [November 7, 2008, 4:09pm UTC](https://www.ruby-forum.com/t/caching-data-in-memory/152743/4 "2008-11-07T16:09:55Z")

</div>

PS. I’m wondering if Rails.cache is better or Memcache. Thanks again.

---

<div class="post-metadata">

**Author:** ![thabenksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thabenksta](https://www.ruby-forum.com/u/thabenksta)\
**Post date:** [November 7, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/caching-data-in-memory/152743/5 "2008-11-07T16:11:01Z")

</div>

On 7 Nov 2008, at 15:00, Ben K. wrote:

> Thanks, Fred.
> 
> Has anyone used Rails.cache.write / read? I wonder how good of a  
> solution that is.

I haven’t used it, but it is a different beast. It is something that  
is shared across all the processes in your cluster, so it’s doing more  
which implies that it will have more overhead then just reading an  
instance variable out of something. It also uses Marshal.dump to dump  
the objects it stores which can introduces some complexities)

Fred

---

<div class="post-metadata">

**Author:** ![thabenksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thabenksta](https://www.ruby-forum.com/u/thabenksta)\
**Post date:** [November 7, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/caching-data-in-memory/152743/6 "2008-11-07T17:46:19Z")

</div>

Thanks again!

Frederick C. wrote:

> On 7 Nov 2008, at 15:09, Ben K. wrote:
> 
> > PS. I’m wondering if Rails.cache is better or Memcache. Thanks again.
> 
> That’s sort of a non question - Rails.cache can be backed by several  
> different stores, one of which is memcache.
> 
> Fred

---

<div class="post-metadata">

**Author:** ![thabenksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thabenksta](https://www.ruby-forum.com/u/thabenksta)\
**Post date:** [November 8, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/caching-data-in-memory/152743/7 "2008-11-08T12:55:43Z")

</div>

Exactly Fred.

Ben, I have had experience using Rails.cache as memcache as the store.  
And it’s worked a treat.

#in environment.rb  
config.cache\_store = :mem\_cache\_store, {:namespace =\> “your\_app”}

It would be best to use the rails built in method because if something  
comes along like a newer or alternative method of caching you only  
have to modify a few bits of your code or even none at all. Just  
update your Rails or gems or whatever. Your caching code doesn’t  
require any modification.

Regards,  
Andrew

On Nov 7, 4:46 pm, Ben K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![thabenksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thabenksta](https://www.ruby-forum.com/u/thabenksta)\
**Post date:** [November 8, 2008, 4:09pm UTC](https://www.ruby-forum.com/t/caching-data-in-memory/152743/8 "2008-11-08T16:09:13Z")

</div>

Thanks a bunch; I’ll use Rails.cache.

---

<div class="post-metadata">

**Author:** ![thabenksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thabenksta](https://www.ruby-forum.com/u/thabenksta)\
**Post date:** [November 7, 2008, 4:13pm UTC](https://www.ruby-forum.com/t/caching-data-in-memory/152743/9 "2008-11-07T16:13:57Z")

</div>

On 7 Nov 2008, at 15:09, Ben K. wrote:

> PS. I’m wondering if Rails.cache is better or Memcache. Thanks again.

That’s sort of a non question - Rails.cache can be backed by several  
different stores, one of which is memcache.

Fred
