- k = uri.to_s
- if !@cache.key?(k)
- remove_stale_cache unless noexpire
- return get(uri, readtimeout, opentimeout, max_redir, true)
- end
- now = Time.new
- begin
- # See if the last-modified header can be used
- # Assumption: the page was not modified if both the header
- # and the cached copy have the last-modified value, and it's the same time
- # If only one of the cached copy and the header have the value, or if the
- # value is different, we assume that the cached copyis invalid and therefore
- # get a new one.
- # On our first try, we tested for last-modified in the webpage first,
- # and then on the local cache. however, this is stupid (in general),
- # so we only test for the remote page if the local copy had the header
- # in the first place.
- if @cache[k].key?(:last_mod)
- h = head(uri, readtimeout, opentimeout, max_redir)
- if h.key?('last-modified')
- if Time.httpdate(h['last-modified']) == @cache[k][:last_mod]
- if h.key?('date')
- @cache[k][:last_use] = Time.httpdate(h['date'])
- else
- @cache[k][:last_use] = now
- end
- @cache[k][:count] += 1
- return @cache[k][:body]
- end
- remove_stale_cache unless noexpire
- return get(uri, readtimeout, opentimeout, max_redir, true)
- end
- remove_stale_cache unless noexpire
- return get(uri, readtimeout, opentimeout, max_redir, true)
- end
- rescue => e
- warning "Error #{e.inspect} getting the page #{uri}, using cache"
- debug e.backtrace.join("\n")
- return @cache[k][:body]
- end
- # If we still haven't returned, we are dealing with a non-redirected document
- # that doesn't have the last-modified attribute
- debug "Could not use last-modified attribute for URL #{uri}, guessing cache validity"
- if noexpire or !expired?(@cache[k], now)