简体   繁体   English

Rails中的第三方API请求缓慢

[英]Slow third party API requests in Rails

I'm building a single page Rails 4 app that uses the Soundcloud and Instagram gems. 我正在构建一个使用Soundcloud和Instagram宝石的单页Rails 4应用程序。 I have noticed that sometimes these API requests slow down my app. 我注意到有时这些API请求会使我的应用变慢。 I am unsure on what the best approach/practice is for handling such requests so that they do not adversely effect the performance of my app? 我不确定处理此类请求的最佳方法/做法是什么,以便它们不会对我的应用程序性能产生不利影响? I've looked into asynchronous requests, sidekiq and have also found this example: http://www.jonb.org/2013/01/25/async-rails.html 我研究了异步请求,sidekiq,还找到了以下示例: http : //www.jonb.org/2013/01/25/async-rails.html

My Index Controller looks like this: 我的索引控制器如下所示:

require 'soundcloud'

class HomeController < ApplicationController

  def index
    # register a new client, which will exchange the username, password for an access_token
    soundcloud_client = Soundcloud.new(
        :client_id => ENV['SOUNDCLOUD_ID'],
        :client_secret => ENV['SOUNDCLOUD_SECRET'],
        :username => ENV['SOUNDCLOUD_USERNAME'],
        :password => ENV['SOUNDCLOUD_PASSWORD']
    )

    # Soundcloud
    @activity = soundcloud_client.get('/me/favorites').first(12)

    # Instagram
    @instagram = Instagram.user_recent_media("1111111", {:count => 12})
  end
end

maybe a better approach to this, is to load your index as you would, but do not load the list from soundcloud on that index. 也许更好的方法是像您那样加载索引,但不要从该索引上的soundcloud加载列表。

Instead have an Ajax call on your view that calls a pure JSON action inside your controller, doing it that way you will serve the page rapidly, and not block on soundclock. 取而代之的是,在视图上进行Ajax调用,以在控制器内调用纯JSON操作,这样便可以快速为页面提供服务,而不会在声音时钟上阻塞。 You can put a spinner on your view indicating that the page is still loading. 您可以在视图上放置微调器,以指示页面仍在加载中。 Looking into jquery load method. 调查jQuery加载方法。

https://api.jquery.com/load/ https://api.jquery.com/load/

good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM