views:

227

answers:

1

Hi,

First of all, let me say I am very new to rails, have been working with it for only a couple of days.

My first application is a little different from traditional: on one of my model classes I have parts of data that is pulled from a database and other part that is acquired by doing an HTTP REST request to an external resource.

I have implemented lazy loading for the "external" resource but, every time the user hits a page that needs one of the fields that are tied to that resource, I re-execute the HTTP request, which obviously doesn't scale.

My question would be: what caching strategy do you think is worth for this scenario? How can I have a cache only for the data that is lazy loaded via HTTP? I'd like to implement a cache that could expire both by time (let's say 5 minutes) or when user executes an action that should invalidate this current cache.

How can I implement it and what would be the memory trade-off to keep those caches? Would it be advisable to keep that data on a session or on a separate structure on the server? Should I consider one of those external caching frameworks like GigaSpaces, etc...?

Thanks in advance for any tips you may offer to this problem.

+1  A: 

I would recommend one of two strategies:

  1. Use memcached, the open-source caching daemon. Client libraries are available for several popular languages, and it supports things like scheduled invalidation of cached data, etc.
  2. Include some fields in your database to store cached data, along with a "cache_last_updated" field which updates every time you save a record. Then, when fetching a record from the database, if this field's value is more than 5 minutes old, you can re-cache the data from the RESTful API (you're not using CouchDB are you?) and save this in the database, updating the last-updated timestamp, and then returning this data to the user.

I'd recommend the first, because frankly otherwise you'll be making several reads and writes to the database per request, incurring a significant performance cost.

Also, this question could apply to things other than Ruby on Rails - a lot of scenarios involve caching from some external source.

zvoase