2013-09-27 43 views
1

如何加速Ping Lastfm api生成的搜索结果?如何提高外部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

不幸的是,不能投票,因为我还没有15个代表。但是,你的回答是杰里米。我们目前正在缓存数据,所以随着更多数据在本地缓存,搜索结果也会改善。但新的搜索仍然相对较慢,我想这超出了我们的控制范围。 – user2821877