Module: MusicBrainz::ClientModules::CachingProxy
- Included in:
- MusicBrainz::Client
- Defined in:
- lib/musicbrainz/client_modules/caching_proxy.rb
Instance Method Summary collapse
Instance Method Details
#cache_path ⇒ Object
4 5 6 |
# File 'lib/musicbrainz/client_modules/caching_proxy.rb', line 4 def cache_path MusicBrainz.config.cache_path end |
#caching? ⇒ Boolean
32 33 34 |
# File 'lib/musicbrainz/client_modules/caching_proxy.rb', line 32 def caching? MusicBrainz.config.perform_caching end |
#clear_cache ⇒ Object
8 9 10 |
# File 'lib/musicbrainz/client_modules/caching_proxy.rb', line 8 def clear_cache FileUtils.rm_r(cache_path) if cache_path && File.exist?(cache_path) end |
#get_contents(url) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/musicbrainz/client_modules/caching_proxy.rb', line 12 def get_contents(url) return super unless caching? hash = Digest::SHA256.hexdigest(url) dir_path = [cache_path, *(0..2).map{ |i| hash.slice(2*i, 2) }].join(?/) file_path = [dir_path, '/', hash.slice(6, 58), '.xml'].join response = { body: nil, status: 500 } if File.exist?(file_path) get_cached_contents(file_path) else response = get_live_contents(file_path, url) if response[:status] == 200 cache_contents!(file_path, response[:body]) end response end end |