簡體   English   中英

是否對Net:HTTP調用/ Rails中的結果進行緩存?

[英]Do Net:HTTP calls/results in Rails get cached?

如果我在Rails中使用Net :: HTTP加載網站的一部分,是否每次都會加載?或者將其與頁腳的其余部分一起緩存?

編輯:我的意思是其余的頁腳當前已緩存。 在頁腳內部呈現的Net:HTTP結果是否也將被緩存? 我希望它每次都能重新加載結果。

不, Net::HTTP不會為您緩存任何內容。 您將必須實現緩存,或使用為您完成緩存的gem。 但是根據您對Rails的處理方式,Rails可以做到這一點-查看片段緩存。

看起來好像不一樣,至少從2011年起默認情況下沒有。在ruby源代碼的net / http.rb文件中也有一個段,其中注釋了以下代碼:

 # The following example performs a conditional GET using the
  # If-Modified-Since header.  If the files has not been modified since the
  # time in the header a Not Modified response will be returned.  See RFC 2616
  # section 9.3 for further details.
  #
     uri = URI('http://example.com/cached_response')
     file = File.stat 'cached_response'

     req = Net::HTTP::Get.new(uri)
     req['If-Modified-Since'] = file.mtime.rfc2822

     res = Net::HTTP.start(uri.hostname, uri.port) {|http|
       http.request(req)
     }

     open 'cached_response', 'w' do |io|
       io.write res.body
     end if res.is_a?(Net::HTTPSuccess)

源文件的日期為7月9日。希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM