开发者

How Would I Load Config From Database And Only Refresh When App Restarted Or By Touching URL

Ok, I have a Rails 3 application and I am using CouchDB as my primary database to take advantage of it's replication capabilities.

Anyway, what I want to do is store some configuration type stuff in 1 document in the database and load the values of this 开发者_如何学运维configuration file one time when the app starts up in production and reload ONLY if the user goes to the admin panel and explicitly requests it to happen. I was thinking by touching a URL to clear the loaded config or something.

My thought was that I would just create a before_filter in application_controller, but since I am new to rails, I didn't know if this was the proper way to do this.

before_filter :get_config

private

def get_config
  @config = Config.get('_id')
end

Clearly this would run every request, which I don't want or need. Is there a way to save the config output so I don't have to fetch it every single request, or is there a better way to do this.

Thanks in advance.


Actually I am writing an article about the proper way of using global variables in rails. This seems to be the case to introduce global variables, as their values are shared across different users.

In your before_filter, try this:

def get_config
  $config ||= Config.get('_id')
end

This would call Config.get('_id') only if $config is false or nil. Otherwise, $config wiil remain unchanged.

The tricky part is global variables (starting with a $ sign) alive in the whole application. So $config is available everywhere (and that would be a problem for careless design!)

Another point is, as you said you are new to rails, I do suggest you to read more about global variables before you use it and DO NOT ADDICT to it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜