开始API缓存 [英] Beginning API caching

查看:72
本文介绍了开始API缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我指出与API响应缓存有关的正确技术.

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.

型号

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

控制器

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

Development.rb

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

之前我没有:null_store ,而我拥有了:memory ,并且收到了 Type Error:Can not dump IO .

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.

需要做什么?就此而言,哪种技术或gem可以处理缓存?有许多航班搜索网站,例如Google.com/flights或Cleartrip.com本身,其结果呈现几乎是瞬时的.您知道任何技巧吗?

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?

谢谢.

推荐答案

这应该做

if response.success?
        response.parsed_response
else

这篇关于开始API缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆