Forum: Ruby on Rails Caching data in memory

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B06f6479558ba7218963e93797d2f928?d=identicon&s=25 Ben Knight (biguniverse)
on 2008-11-07 00:28
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-07 10:31
(Received via mailing list)
On 6 Nov 2008, at 23:28, Ben Knight 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
B06f6479558ba7218963e93797d2f928?d=identicon&s=25 Ben Knight (biguniverse)
on 2008-11-07 16:00
Thanks, Fred.

Has anyone used Rails.cache.write / read?  I wonder how good of a
solution that is.
B06f6479558ba7218963e93797d2f928?d=identicon&s=25 Ben Knight (biguniverse)
on 2008-11-07 16:09
PS.  I'm wondering if Rails.cache is better or Memcache. Thanks again.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-07 16:11
(Received via mailing list)
On 7 Nov 2008, at 15:00, Ben Knight 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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-07 16:13
(Received via mailing list)
On 7 Nov 2008, at 15:09, Ben Knight 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
B06f6479558ba7218963e93797d2f928?d=identicon&s=25 Ben Knight (biguniverse)
on 2008-11-07 17:46
Thanks again!

Frederick Cheung wrote:
> On 7 Nov 2008, at 15:09, Ben Knight 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
0333c8c993f63d263c9bc59ad2c35a9b?d=identicon&s=25 chalkers (Guest)
on 2008-11-08 12:55
(Received via mailing list)
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 Knight <rails-mailing-l...@andreas-s.net>
B06f6479558ba7218963e93797d2f928?d=identicon&s=25 Ben Knight (biguniverse)
on 2008-11-08 16:09
Thanks a bunch; I'll use Rails.cache.
This topic is locked and can not be replied to.