<commit_before>class Metadata < Bolognese::Metadata
  # fetching and parsing is an expensive step, so we cache using the crosscite format
  def initialize(input: nil, from: nil, format: nil, **options)
    # don't cache if passing thru metadata or when in Test mode
    return super(input: input, from: from, sandbox: options[:sandbox]) if from == format.to_s || %w(test).include?(Rails.env)

    cached_input = Rails.cache.read(input)

    if cached_input
      super(input: cached_input, from: "crosscite")
    else
      super(input: input)
      Rails.cache.write(input, self.crosscite, raw: true)
    end
  end
end
<commit_msg>cache datacite xml<commit_after>class Metadata < Bolognese::Metadata
  # fetching and parsing is an expensive step, so we cache using the crosscite format, or raw xml
  def initialize(input: nil, from: nil, format: nil, **options)
    # don't cache if passing thru metadata or when in Test mode
    return super(input: input, from: from, sandbox: options[:sandbox]) if %w(test).include?(Rails.env)

    # passthru of DataCite or Crossref xml
    if from == format.to_s
      cached_input = Rails.cache.read("#{from}/#{input}")

      if cached_input
        super(input: cached_input, from: from)
      else
        super(input: input)
        Rails.cache.write("#{from}/#{input}", self.raw, raw: true)
      end
    else
      cached_input = Rails.cache.read("crosscite/#{input}")

      if cached_input
        super(input: cached_input, from: "crosscite")
      else
        super(input: input)
        Rails.cache.write("crosscite/#{input}", self.crosscite, raw: true)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11206368646662584, 'fraction_numerical': 0.0, 'mean_word_length': 3.322751322751323, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6399888', 'n_tokens_mistral': 513, 'n_tokens_neox': 502, 'n_words': 160}