class Hiptest::ExportCache
Public Class Methods
new(cache_dir, cache_duration, reporter: nil)
click to toggle source
# File lib/hiptest-publisher/export_cache.rb, line 9 def initialize(cache_dir, cache_duration, reporter: nil) @cache_dir = cache_dir @cache_duration = cache_duration @reporter = reporter || Reporter.new clear_cache end
Public Instance Methods
cache(url, content, date: nil)
click to toggle source
# File lib/hiptest-publisher/export_cache.rb, line 17 def cache(url, content, date: nil) return if @cache_duration <= 0 date ||= Time.now filename = "#{Digest::MD5.hexdigest(url)}-#{date.to_i}" file_writer.write_to_file(File.join(@cache_dir, filename), I18n.t("caching_data")) { content } end
cache_for(url)
click to toggle source
# File lib/hiptest-publisher/export_cache.rb, line 26 def cache_for(url) return if @cache_duration <= 0 hashed_url = Digest::MD5.hexdigest(url) expiry_date = (Time.now - @cache_duration).to_i cached_filename = cached_filenames .select do |filename | filename.start_with?("#{hashed_url}-") && !expired?(filename, expiry_date) end .sort do | f1, f2| timestamp_from_filename(f1) <=> timestamp_from_filename(f2) end .last return nil if cached_filename.nil? full_path = File.join(@cache_dir, cached_filename) @reporter.show_verbose_message(I18n.t(:using_cache, full_path: full_path)) File.read(full_path) end
clear_cache()
click to toggle source
# File lib/hiptest-publisher/export_cache.rb, line 48 def clear_cache expired_files.map do |filename| FileUtils.rm(File.join(@cache_dir, filename)) end end
Private Instance Methods
cached_filenames()
click to toggle source
# File lib/hiptest-publisher/export_cache.rb, line 68 def cached_filenames Dir .entries(@cache_dir) .select { |entry| File.file?(File.join(@cache_dir, entry)) } rescue Errno::ENOENT => err [] end
expired?(filename, expiry_date)
click to toggle source
# File lib/hiptest-publisher/export_cache.rb, line 76 def expired?(filename, expiry_date) timestamp_from_filename(filename) < expiry_date end
expired_files()
click to toggle source
# File lib/hiptest-publisher/export_cache.rb, line 60 def expired_files expiry_date = (Time.now - @cache_duration).to_i cached_filenames.select do |filename | expired?(filename, expiry_date) end end
file_writer()
click to toggle source
# File lib/hiptest-publisher/export_cache.rb, line 56 def file_writer FileWriter.new(@reporter) end
timestamp_from_filename(filename)
click to toggle source
# File lib/hiptest-publisher/export_cache.rb, line 80 def timestamp_from_filename(filename) m = filename.match(/\A[a-f0-9]{32}-(\d+)\Z/) m.nil? ? nil : m[1].to_i end