Beginning API caching

2019-08-07 01:54发布

Can someone point to me the correct technique wch relates to caching of API responses.

I have been thru lots of existing questions, guides, posts but somewhere some crucial element is not working right.

Model

class Cleartrip
  include HTTParty

  debug_output $stdout

  base_uri "api.staging.cleartrip.com/air/1.0/search"
  headers 'X-CT-API-KEY' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
  format :xml

  def self.get_flight(url)
    Rails.cache.fetch(url, :expires => 1.hour) do
      response = get(url) 
      if response.success?
        response
      else
        raise response.message
      end
    end

  end
end

Controller

@flight = Cleartrip.get_flight("?from=DEL&to=BLR&depart-date=2014-08-10&adults=1&children=0&infants=0")

Development.rb

  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = true
  config.cache_store = :null_store

Instead of :null_store, previously I had :memory and I received Type Error: Can't dump IO.

Even with the current configs, on every reload it hits the server and takes a lot of time to display the result.

What is it that needs to be done? Which technique or gem, for that matter, takes care of caching? There are many flight search sites, like Google.com/flights or Cleartrip.com itself, there rendering of results is almost instantaneous. Do you know of any tips and tricks?

Thanks.

1条回答
贼婆χ
2楼-- · 2019-08-07 02:10

This should do

if response.success?
        response.parsed_response
else
查看更多
登录 后发表回答