开发者

Ruby on Rails architecture: Pre-generating a View for seldom-updated data

I'm creating an application that polls a number of sensors every X minutes and adds the new readings, with a timestamp, in the database. The latest data can be viewed via webpage.

Currently, I have implemented this with two separate processes:

  1. Cron job with Whenever, to poll the sensors and update the db, via the Model
  2. View (with associated Controller and Model) to pull the latest data off the database and display it.

Since the data only gets updated every X minutes, I was thinking it's probably more effective to pre-generate the View as static HTML, every time the sensors are polled. That way I reduce database accesses开发者_高级运维 by many orders of magnitude.

Does this sound like a good idea? What would be the best way to pre-generate the view with Rails?


You're basically describing caching, which Rails already does for you. If the computed output page doesn't change, then Rails doesn't re-do it every time, it just gives you the cached version. This is automatic, and you don't have to think about it unless you've explicitly turned it off.

It's also possible that whatever web server you're using does something similar as well.

However, by default, in the development environment caching is turned off, and views are reloaded every time you refresh the page. So, if you're worried that caching is off in development, and therefore it will be off in production, that shouldn't be the case. Caching is turned off in development intentionally, so you can be modifying your app and hitting refresh in your browser to see the differences on the fly, without having to restart your development web server (Webrick, or mongrel, or whatever you're using).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜