Cache data set on the server

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.

On Feb 22, 9:33am, Ram K. [email protected] 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

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 C. <

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.