class UrlReader::FileCache
Public Class Methods
new(cache_dir_path)
click to toggle source
# File lib/url_reader/file_cache.rb, line 2 def initialize(cache_dir_path) @cache_dir_path = cache_dir_path @cache = {} end
Public Instance Methods
read_entry(key)
click to toggle source
# File lib/url_reader/file_cache.rb, line 7 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
write_entry(key, value)
click to toggle source
# File lib/url_reader/file_cache.rb, line 22 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 Instance Methods
decoded_value(value)
click to toggle source
# File lib/url_reader/file_cache.rb, line 41 def decoded_value(value) CGI.unescape(value) end
encoded_key(key)
click to toggle source
# File lib/url_reader/file_cache.rb, line 33 def encoded_key(key) URI.encode_www_form_component(key) end
encoded_value(value)
click to toggle source
# File lib/url_reader/file_cache.rb, line 37 def encoded_value(value) CGI.escape(value) end
hash(key)
click to toggle source
# File lib/url_reader/file_cache.rb, line 45 def hash(key) Digest::SHA256.hexdigest(key) end