Forum: Ruby on Rails Cache data set on the server

03adc5fbb7761c4adaff82cc57008014?d=identicon&s=25 Ram Kumar (lewstherin)
on 2011-02-22 10:33
Hi,

I am new to Rails and I am building a rails server which acts as the
backend for a mobile app. I have certain daemon processes that fetch and
store data in a database. Every time someone makes a request for data
from the phone, I will have to send him the same dataset irrespective of
who is requesting the data.

This dataset will keep changing quite frequently and my daemons will
keep updating the database accordingly. Since the same data is to be
sent to every one, I am assuming that it would be best to cache the data
on the server and keep updating the cache at a set frequency (thus
making only one trip to the db). When I get a request from the phone, I
just retrieve this cached dataset and send it back, w/o hitting the db.

I think one way of doing this would be to use a singleton model class
which will cache the data and this would act as the source. Is this the
best way to go about this process? Does Rails provide some other means
of doing this?

Thanks,

Ramkumar.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2011-02-22 11:01
(Received via mailing list)
On Feb 22, 9:33am, Ram Kumar <li...@ruby-forum.com> wrote:
> sent to every one, I am assuming that it would be best to cache the data
> on the server and keep updating the cache at a set frequency (thus
> making only one trip to the db). When I get a request from the phone, I
> just retrieve this cached dataset and send it back, w/o hitting the db.
>
> I think one way of doing this would be to use a singleton model class
> which will cache the data and this would act as the source. Is this the
> best way to go about this process? Does Rails provide some other means
> of doing this?

Rails provides Rails.cache which is an abstraction round several
different data stores. Personally i wouldn't usually recommend the in
memory cache you're considering. It means that you have as many copies
of the data in memories as you have mongrels/passenger instances etc,
ie you are fetching the data more often than you need. It also makes
it difficult for you to force expiry of the cache. Most people use
something like memcache for this job (one of the cache stores
Rails.cache supports is a memcache store).

You may also want to consider caching at different levels in the
stack, for example by putting something like varnish in front of your
web servers.

Fred
3ca98e75ecd686304f9a4aae2b25ac09?d=identicon&s=25 Chris Kottom (Guest)
on 2011-02-22 11:09
(Received via mailing list)
Regarding the daemon part of your application, I've done something
similar
before, and used a combination of Rails-based rake tasks triggered by
Chronic or something similar.  I didn't have the need to manage the
cache as
you're describing, but including a task that refreshes cached
information
and running it after each update would probably be simple enough.

On Tue, Feb 22, 2011 at 10:59 AM, Frederick Cheung <
03adc5fbb7761c4adaff82cc57008014?d=identicon&s=25 Ram Kumar (lewstherin)
on 2011-02-23 12:15
Hi,

First off thank you both for your replies and apologies for the delayed
reply.

@Fred: I don't know about passenger/ multiple mongrel instances yet and
will look that up. I will also explore Rails:Cache and see what I can
come up.

@Chris: I was going to do some cron jobs which send requests to the
server. Rake tasks seems to be a much better way. I will explore that
too.

Once again thanks for your help in pointing me in the right direction.

Ram.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.