Added cache_contents block around get_contents to cache requests when a cache path is set.
This commit is contained in:
parent
7600b3855b
commit
abc8218a06
@ -1,6 +1,7 @@
|
|||||||
module MusicBrainz
|
module MusicBrainz
|
||||||
@@last_query_time = 0
|
@@last_query_time = 0
|
||||||
@@query_interval = 1.5 # Min: 1.0 Safe: 1.5
|
@@query_interval = 1.5 # Min: 1.0 Safe: 1.5
|
||||||
|
@@cache_path = nil
|
||||||
|
|
||||||
WEB_SERVICE_URL = 'http://musicbrainz.org/ws/2/'
|
WEB_SERVICE_URL = 'http://musicbrainz.org/ws/2/'
|
||||||
USER_AGENT = "gem musicbrainz (https://github.com/magnolia-fan/musicbrainz) @ " + Socket.gethostname
|
USER_AGENT = "gem musicbrainz (https://github.com/magnolia-fan/musicbrainz) @ " + Socket.gethostname
|
||||||
@ -9,18 +10,55 @@ module MusicBrainz
|
|||||||
@@query_interval = sec.to_f
|
@@query_interval = sec.to_f
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.cache_path= path
|
||||||
|
@@cache_path = path
|
||||||
|
end
|
||||||
|
|
||||||
def self.load resourse, params = {}
|
def self.load resourse, params = {}
|
||||||
url = WEB_SERVICE_URL + resourse.to_s.gsub('_', '-') + '/' + (params[:id].to_s || '')
|
url = WEB_SERVICE_URL + resourse.to_s.gsub('_', '-') + '/' + (params[:id].to_s || '')
|
||||||
params.delete(:id) unless params[:id].nil?
|
params.delete(:id) unless params[:id].nil?
|
||||||
url << '?' + params.map{ |k, v|
|
url << '?' + params.map{ |k, v|
|
||||||
k.to_s.gsub('_', '-') + '=' + (v.is_a?(Array) ? v.map{ |_| _.to_s.gsub('_', '-') }.join('+') : v.to_s)
|
k.to_s.gsub('_', '-') + '=' + (v.is_a?(Array) ? v.map{ |_| _.to_s.gsub('_', '-') }.join('+') : v.to_s)
|
||||||
}.join('&') unless params.empty?
|
}.join('&') unless params.empty?
|
||||||
self.get_contents url
|
self.cache_contents(url) do
|
||||||
|
self.get_contents url
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.clear_cache
|
||||||
|
FileUtils.rm_r(@@cache_path) if @@cache_path && File.exist?(@@cache_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def self.cache_contents url
|
||||||
|
response = nil
|
||||||
|
url_parts = url.split('/')
|
||||||
|
file_name = url_parts.pop
|
||||||
|
directory = url_parts.pop
|
||||||
|
file_path = @@cache_path ? "#{@@cache_path}/#{directory}/#{file_name}" : nil
|
||||||
|
|
||||||
|
if file_path && File.exist?(file_path)
|
||||||
|
response = File.open(file_path).gets
|
||||||
|
else
|
||||||
|
response = yield
|
||||||
|
|
||||||
|
unless response.nil? || file_path.nil?
|
||||||
|
FileUtils.mkdir_p file_path.split('/')[0..-2].join('/')
|
||||||
|
file = File.new(file_path, 'w')
|
||||||
|
file.puts(response.gets.force_encoding('UTF-8'))
|
||||||
|
file.chmod(0755)
|
||||||
|
file.close
|
||||||
|
response.rewind
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
response
|
||||||
|
end
|
||||||
|
|
||||||
def self.get_contents url
|
def self.get_contents url
|
||||||
|
self.get_contents_intern
|
||||||
|
|
||||||
response = nil
|
response = nil
|
||||||
|
|
||||||
5.times do
|
5.times do
|
||||||
@ -31,7 +69,7 @@ private
|
|||||||
response = open(url, "User-Agent" => USER_AGENT)
|
response = open(url, "User-Agent" => USER_AGENT)
|
||||||
@@last_query_time = Time.now.to_f
|
@@last_query_time = Time.now.to_f
|
||||||
rescue => e
|
rescue => e
|
||||||
return nil if e.io.status[0].to_i == 404
|
response = nil if e.io.status[0].to_i == 404
|
||||||
end
|
end
|
||||||
|
|
||||||
break unless response.nil?
|
break unless response.nil?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user