开发者

Fetch third party data and store in rails application variable

开发者 https://www.devze.com 2022-12-24 06:47 出处:网络
I need to do periodical background task fetching data from a weather site. How in rails can I fetch json from a remote url and store it on the server? Also, there doesn\'t seem to be any point in st

I need to do periodical background task fetching data from a weather site.

How in rails can I fetch json from a remote url and store it on the server? Also, there doesn't seem to be any point in storing this in a db so h开发者_高级运维ow in rails can I store a variable available to all users?


Why don't you store the data in the cache (local OR memcached).

Write/update the cache when you retrieve the feed data:

def load_feed
  Rails.cache.write("feed_data", get_data_from_feed)
end

Read from cache, when you need to access the data:

def read_feed
  Rails.cache.fetch("feed_data") { get_data_from_feed }
end


I will only partialy answer your question.

If you want to store it as some variable availble to all users, then probably you can create a my_new_variable.rb in config/initializers. And you can generate some code that defines and initializes your variable (maybe constant is better). Then is a bad part of this approach - you have to restart your server. If you are using passanger, then just touch tmp/restart.txt and it's done.

You can also store it in yml file and load it on server start.

Even if you would store it in a different way, probably the easiest way is to restart server to load this new variable. Otherwise on every request you should check if there is new variable availble (on example check last update time on a file), or reload this file.

So, for me it looks like the easiest way is to store it in db.

0

精彩评论

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