class ResultsCache
  def initialize(date, descriptor:)
    @cache_descriptor = descriptor
  end

  def filename
    @cache_descriptor.filename
  end

  def cached
    filename = @cache_descriptor.filename

    cacheable = @cache_descriptor.data_fixed? || @cache_descriptor.temporary_cache_fresh?

    if cacheable
      if File.exist?(filename)
        return File.read(filename)
      else
        data = yield

        File.open(filename, "w") do |file|
          file.write(data)
        end

        return data
      end
    else
      FileUtils.rm_f(filename)

      yield
    end
  end
end

class ResultsCache
  def initialize(date, descriptor:)
    @cache_descriptor = descriptor
  end

  def filename
    @cache_descriptor.filename
  end

  def cached
    filename = @cache_descriptor.filename

    cacheable = @cache_descriptor.data_fixed? || @cache_descriptor.temporary_cache_fresh?

    if cacheable
      if File.exist?(filename)
        if @cache_descriptor.data_fixed?
          STDOUT.puts "Reading from cache: #{filename}"
        elsif @cache_descriptor.temporary_cache_fresh?
          STDOUT.puts "Reading from temporary cache: #{filename}"
        end

        return File.read(filename)
      else
        STDOUT.puts "Writing to cache: #{filename}"

        data = yield

        File.open(filename, "w") do |file|
          file.write(data)
        end

        return data
      end
    else
      STDOUT.puts "Deleting stale temporary cache: #{filename}"
      FileUtils.rm_f(filename)

      yield
    end
  end
end

Add logging to stdout from ResultsCache.
Add logging to stdout from ResultsCache.
