Class: Hiptest::ExportCache
- Inherits:
-
Object
- Object
- Hiptest::ExportCache
- Defined in:
- lib/hiptest-publisher/export_cache.rb
Instance Method Summary collapse
- #cache(url, content, date: nil) ⇒ Object
- #cache_for(url) ⇒ Object
- #clear_cache ⇒ Object
-
#initialize(cache_dir, cache_duration, reporter: nil) ⇒ ExportCache
constructor
A new instance of ExportCache.
Constructor Details
#initialize(cache_dir, cache_duration, reporter: nil) ⇒ ExportCache
Returns a new instance of ExportCache.
9 10 11 12 13 14 15 |
# 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 |
Instance Method Details
#cache(url, content, date: nil) ⇒ Object
17 18 19 20 21 22 23 24 |
# 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) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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| (f1) <=> (f2) end .last return nil if cached_filename.nil? full_path = File.join(@cache_dir, cached_filename) @reporter.(I18n.t(:using_cache, full_path: full_path)) File.read(full_path) end |
#clear_cache ⇒ Object
48 49 50 51 52 |
# 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 |