<commit_before>class MusicSearch < ActiveRecord::Base
  extend Textacular

  attr_writer :query
  attr_accessible :query
  belongs_to :result, polymorphic: true

  def self.search(query)
    super.preload(:result).map(&:result)
  end
end<commit_msg>Add basic caching to results<commit_after>class MusicSearch < ActiveRecord::Base
  extend Textacular

  attr_writer :query
  attr_accessible :query
  belongs_to :result, polymorphic: true

  def self.search(query)
    Rails.cache.fetch([cache_key, query]) do
      super.preload(:result).map(&:result)
    end
  end

  def self.cache_key
    ['MusicSearch', Album.maximum(:updated_at).to_i, Artist.maximum(:updated_at).to_i, Song.maximum(:updated_at).to_i].join('/')
  end
end
{'fraction_non_alphanumeric': 0.11310344827586206, 'fraction_numerical': 0.0, 'mean_word_length': 4.854838709677419, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6783747', 'n_tokens_mistral': 245, 'n_tokens_neox': 229, 'n_words': 50}