Sha256: 3c8f68ab777493a9af36aa0d5a052a7dc255653b3282ee01bf17b9eca78fa756

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

class UrlReader::FileCache
  def initialize(cache_dir_path)
    @cache_dir_path = cache_dir_path
    @cache = {}
  end

  def read_entry(key)
    unless @cache.has_key?(key)
      ekey = encoded_key(key)
      hash = hash(ekey)
      file_path = File.join(@cache_dir_path, hash)
      value = nil
      if File.exist?(file_path)
        value = (decoded_value(File.open(file_path).read.strip.split("\n")
                                 .select { |x| x.start_with?("#{ekey}\t") }[0].split("\t", 2)[1]) rescue nil)
      end
      @cache[key] = value
    end
    @cache[key]
  end

  def write_entry(key, value)
    @cache[key] = value
    ekey = encoded_key(key)
    hash = hash(ekey)
    file_path = File.join(@cache_dir_path, hash)
    File.open(file_path, 'a') { |f| f.puts("#{ekey}\t#{encoded_value(value)}") }
    true
  end

  private

  def encoded_key(key)
    URI.encode_www_form_component(key)
  end

  def encoded_value(value)
    CGI.escape(value)
  end

  def decoded_value(value)
    CGI.unescape(value)
  end

  def hash(key)
    Digest::SHA256.hexdigest(key)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
url_reader-0.2.5 lib/url_reader/file_cache.rb
url_reader-0.2.4 lib/url_reader/file_cache.rb
url_reader-0.2.3 lib/url_reader/file_cache.rb
url_reader-0.2.2 lib/url_reader/file_cache.rb
url_reader-0.2.1 lib/url_reader/file_cache.rb
url_reader-0.2.0 lib/url_reader/file_cache.rb