Reputation: 413
We have a rails 3 controller action that calls a remote api & caches the response. Unfortunately it's a slow process, so the first person to call that action has to wait quite a long time for the response. How can I call this controller action when rails starts (or at some point soon after) so the first user does not have to have that long delay.
Thanks in advance
Upvotes: 1
Views: 107
Reputation: 24541
If you want to load some data on startup, I recommend doing that in an initializer, setting a variable somewhere appropriate, and then having your controller just read from that variable.
But now you're hitting the remote API for each app server instance. Also refreshing the cache in each of those instances will be challenging. A better approach would be to stash the data in a shared store like Redis or Memcached, and then set it using a cron job, e.g. via the whenever
gem. Rails is not really designed for inter-request caching.
Upvotes: 2