如何提高外部API的搜索速度?

时间:2013-09-27 03:22:21

标签: ruby-on-rails api ruby-on-rails-4 last.fm

如何加快ping Lastfm api所产生的搜索结果?

以下是我们正在使用的代码:

def self.search(term)
    LastfmAPI.artist_search(term).map { |a| Lastfm::Artist.new(a) }
  end

  # Name and lastfm_id are synonyms                                                                                                                           
  def name
    self.lastfm_id
  end

  def past_events(geo=nil, options={})
    events = self.events.past
    lastfm_count = LastfmAPI.artist_getPastEvents_count(self.lastfm_id)

    # Check if database is current                                                                                                                            
    if events.count == lastfm_count # TODO: && the first event itself matches entirely                                                                        
      # TODO: extract above comparison to method                                                                                                              

      # return only those in the correct radius                                                                                                               
      events = events.in_radius(geo) if geo.present?
    else
      # if not current, make array of Lastfm::Event objects from API call                                                                                     
      events = LastfmAPI.artist_getPastEvents_all(self.lastfm_id, lastfm_count).map do |e|
    Saver::Events.perform_async(e) # send to worker to save to database                                                                                   

    Lastfm::Event.new(e)
      end

1 个答案:

答案 0 :(得分:1)

当您依赖外部服务时,您无法加快实际服务的执行速度。您可以做的最好的事情是在您自己的应用程序中本地缓存内容,这样您就不会经常往返。