【问题标题】:Slow third party API requests in RailsRails 中缓慢的第三方 API 请求
【发布时间】:2014-04-16 13:10:16
【问题描述】:

我正在构建一个使用 Soundcloud 和 Instagram gems 的单页 Rails 4 应用程序。我注意到有时这些 API 请求会减慢我的应用程序的速度。我不确定处理此类请求的最佳方法/做法是什么,以免它们对我的应用程序的性能产生不利影响?我研究了异步请求,sidekiq,还找到了这个例子:http://www.jonb.org/2013/01/25/async-rails.html

我的索引控制器如下所示:

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

【问题讨论】:

    标签: ruby-on-rails api ruby-on-rails-4 soundcloud instagram


    【解决方案1】:

    也许一个更好的方法是加载你的索引,但不要从 soundcloud 加载该索引上的列表。

    取而代之的是,在您的视图上调用 Ajax 调用,在控制器中调用纯 JSON 操作,这样您将快速为页面提供服务,而不会阻塞 soundclock。您可以在视图上放置一个微调器,指示页面仍在加载。调查 jquery 加载方法。

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

    祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-25
      • 2017-04-30
      • 2020-06-27
      • 2019-08-10
      • 2017-09-20
      • 1970-01-01
      • 2021-12-07
      • 2018-01-10
      相关资源
      最近更新 更多