带有expires_in的Rails.cache.fetch只有在新值不为nil时才会过期

我想做一个简单的Rails.cache.fetch并在大约10分钟后过期。 缓存中充满了来自外部API的json数据。 但是,有时无法访问外部API。 因此,当缓存过期并尝试获取新的json数据时,缓存内容将变为无效。

如果fetch_json返回有效数据,我怎样才能使Rails.cache.fetch只对EXPQRE缓存? 但是,如果收到新的有效数据,缓存应在10分钟后过期。

这是我尝试这样做的方式,但它不起作用。 更好的代码突出显示在这个要点: https : //gist.github.com/i42n/6094528

任何提示我如何才能完成这项工作?

 module ExternalApiHelper require 'timeout' require 'net/http' def self.fetch_json(url) begin result = Timeout::timeout(2) do # 2 seconds # operation that may cause a timeout uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) return JSON.parse(response.body) end return result rescue # if any error happens in the block above, return empty string # this will result in fetch_json_with_cache using the last value in cache # as long as no new data arrives return "" end end def self.fetch_json_with_cache(url, expire_time) cache_backup = Rails.cache.read(url) api_data = Rails.cache.fetch(url, :expires_in => expire_time) do new_data = fetch_json(url) if new_data.blank? # if API fetch did not return valid data, return the old cache state cache_backup else new_data end end return api_data end end 

解决了这个问题。 也许不是最优雅的方式,但有效: https : //github.com/ReliveRadio/reliveradio-website/blob/4874cf4158361c73a693e65643d9e7f11333d9d6/app/helpers/external_api_helper.rb