W
W
withkittens2014-12-03 17:33:30
Ruby on Rails
withkittens, 2014-12-03 17:33:30

Download a resource every 10 minutes and cache it?

Let's say there is an external API: someexternalsvc.org/products.json. The controller must use this data to somehow process (for example, build a beautiful plate on it) and display it to the user. It is clear that pulling the external API for each request is wasteful (traffic, cap for the number of calls, and the data is rarely updated), so you can update the data every 10 minutes.
How are things like this implemented in production?
Since I am almost new to rails, I tried to google it. Current guess: you need to write a rake task that will be executed on an external timer (for example, on crony) and load json.
Where and how to store (cache) the downloaded data? Is there a way without writing to the database or to files (data ~ for a couple of KB)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@mgyk, 2014-12-03
@withkittens

Just wrap it in a rail cache. Will use files by default (can be switched to memcache)

Rails.cache.fetch("products", :expires_in => 10.minutes) do
   ....
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question