What is the best caching strategy for this problem?

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

My first application is a little different from the traditional one: on one of my model classes, I have pieces of data that are fetched from the database and another piece that is obtained by making an HTTP REST request to an external resource.

I've implemented lazy loading for an "external" resource, but every time the user clicks on a page that needs one of the fields bound to that resource, I re-execute the HTTP request, which obviously doesn't scale.

My question is, what caching strategy do you think is worthwhile for this scenario? How can I only use the cache for data that is lazy loaded over HTTP? I would like to implement a cache that can expire both in time (let's say 5 minutes) or when the user performs an action that should invalidate the current cache.

How can I implement it and what would be the memory tradeoff for storing these caches? Would it be advisable to store this data in a session or on a separate structure on the server? Should I consider one of these external caching systems like GigaSpaces, etc ??

Thanks in advance for any advice you can offer on this issue.

+1


source to share


1 answer


I would recommend one of two strategies:

  • Use memcached , an open source caching daemon. The client libraries are available for several popular languages ​​and support features such as scheduled invalidation of cached data, etc.
  • Include some fields in your database for storing cached data, along with a "cache_last_updated" field that is updated every time a record is saved. Then, by fetching a record from the database, if that field value is more than 5 minutes, you can re-cache the data from the RESTful API (you are not using CouchDB, are you?) And store that in the database, updating the last updated timestamp, and then returning this data to the user.


I would recommend first, because frankly, otherwise you will be doing multiple reads and writes to the database on demand, which will result in significant performance costs.

Also, this question may be related to things other than Ruby on Rails - many scenarios involve caching from some external source.

+1


source







All Articles