def get_cached(uri_or_str, readtimeout=10, opentimeout=5,
max_redir=@bot.config['http.max_redir'],
noexpire=@bot.config['http.no_expire_cache'])
if uri_or_str.class <= URI
uri = uri_or_str
else
uri = URI.parse(uri_or_str.to_s)
end
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
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
debug "Could not use last-modified attribute for URL #{uri}, guessing cache validity"
if noexpire or !expired?(@cache[k], now)
@cache[k][:count] += 1
@cache[k][:last_use] = now
debug "Using cache"
return @cache[k][:body]
end
debug "Cache expired, getting anew"
@cache.delete(k)
remove_stale_cache unless noexpire
return get(uri, readtimeout, opentimeout, max_redir, true)
end