Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Beginning API caching

API Caching Ruby Ruby on Rails Ruby on Rails 3 Ruby on Rails 4

  • Please log in to reply
4 replies to this topic

#1 arjunm009

arjunm009

    Signalman

  • Members
  • 17 posts
  • Locationindia

Posted 03 August 2014 - 12:35 PM

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_store 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.



#2 Ohm

Ohm

    Driver

  • Moderators
  • 360 posts
  • LocationCopenhagen

Posted 03 August 2014 - 02:04 PM

The response you are returning isn't cachable, as it tells you, it's an IO object. Call #body on it to get the actual content of the call.


Blog: http://ohm.sh | Twitter: @madsohm

#3 arjunm009

arjunm009

    Signalman

  • Members
  • 17 posts
  • Locationindia

Posted 04 August 2014 - 08:40 AM

That is something like this?

def self.get_flight
url = "?from=DEL&to=BLR&depart-date=2014-08-10&adults=1&children=0&infants=0"
response = get(url)
Rails.cache.fetch(response, :expires => 1.hour) do

   response = response.body 

end
end


#4 Ohm

Ohm

    Driver

  • Moderators
  • 360 posts
  • LocationCopenhagen

Posted 04 August 2014 - 08:44 AM

I would've done it just like you had it already, but with response.body instead of response.

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.body
      else
        raise response.message
      end
    end
  end
end

Blog: http://ohm.sh | Twitter: @madsohm

#5 arjunm009

arjunm009

    Signalman

  • Members
  • 17 posts
  • Locationindia

Posted 04 August 2014 - 01:13 PM

Yup it works now with :memory_store. That was cool! Instead of "response.body" i did "response.parsed_response".

Now i see where more mod is required.

1. How can i make changes made in the model file reflect in the view. 

    I had to change some parameters in the "url", to see modifications. 

2. How can I ensure the data would not be stale, like checking etag or expires header data.

3. The response shown in the view is in some random order. Can it be made to sort in the model by suppose "total_fare", as default.     I know it can be done in the view, but like to know if at all it is possible in the model and how?

 

( a portion of parsed_response - http://jsfiddle.net/PP9N5/ )







Also tagged with one or more of these keywords: API, Caching, Ruby, Ruby on Rails, Ruby on Rails 3, Ruby on Rails 4

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users