lib/httpx/plugins/response_cache/file_store.rb in httpx-0.23.4 vs lib/httpx/plugins/response_cache/file_store.rb in httpx-0.24.0

- old
+ new

@@ -1,34 +1,39 @@ -# # frozen_string_literal: true +# frozen_string_literal: true -# require "mutex_m" -# require "forwardable" +require "pathname" -# module HTTPX::Plugins -# module ResponseCache -# class FileStore < Store -# def initialize(dir = Dir.tmpdir) -# @dir = Pathname.new(dir) -# @store = {} -# @hash_store.extend(Mutex_m) -# end +module HTTPX::Plugins + module ResponseCache + class FileStore < Store + def initialize(dir = Dir.tmpdir) + @dir = Pathname.new(dir) + end -# def clear -# @store.synchronize { @store.clear } -# end + def clear + # delete all files + end -# def _get(request) -# return unless File.exist?(@dir.join(request.response_cache_key)) + def cached?(request) + file_path = @dir.join(request.response_cache_key) -# @store.synchronize { @store[key] } -# end + exist?(file_path) + end -# def set(key) -# @store.synchronize do -# resps = (@store[key] ||= []) + private -# yield resps -# end -# end -# end -# end -# end + def _get(request) + return unless cached?(request) + + File.open(@dir.join(request.response_cache_key)) + end + + def _set(request, response) + file_path = @dir.join(request.response_cache_key) + + response.copy_to(file_path) + + response.body.rewind + end + end + end +end