<commit_before>## This controller only handles showing articles and the feed.
class ArticlesController < ApplicationController
  def index
    @articles = Rails.cache.fetch("#{cache_key}/current") do
      Article.all.order('created_at DESC, updated_at DESC').first(3)
    end
  end

  def archives
    page_size = 50

    @number_of_pages = (1.0 * Article.count / page_size).ceil.abs

    @page_number = archive_page_number(@number_of_pages)

    @articles = Rails.cache.fetch("#{cache_key}/archives") do
      Article.all.order('created_at DESC').to_a.slice(page_size * @page_number,
                                                      page_size)
    end
  end

  def colophon
  end

  def show
    @article = Rails.cache.fetch("#{cache_key}/#{params[:id]}") do
      Article.friendly.find(params[:id])
    end
  end

  def feed
    @articles = Rails.cache.fetch("#{cache_key}/feed") do
      Article.all.order('created_at DESC, updated_at DESC')
    end

    respond_to do |format|
      format.html
      format.rss { render layout: false }
    end
  end

  private

  def article_params
    params.require(:article).permit(:title, :text)
  end

  def archive_page_number(number_of_pages)
    page_number = params[:page].nil? ? 1 : params[:page].to_i.abs
    page_number = page_number > 0 ? page_number : 1
    [number_of_pages, page_number].min
  end

  def cache_key
    count = Article.count
    max_updated_at = Article.maximum(:updated_at).try(:utc).try(:to_s, :number)
    "articles/all-#{count}-#{max_updated_at}"
  end
end<commit_msg>Added cache check to archives<commit_after>## This controller only handles showing articles and the feed.
class ArticlesController < ApplicationController
  def index
    @articles = Rails.cache.fetch("#{cache_key}/current") do
      Article.all.order('created_at DESC, updated_at DESC').first(3)
    end
  end

  def archives
    page_size = 50

    @number_of_pages = (1.0 * Article.count / page_size).ceil.abs

    @page_number = archive_page_number(@number_of_pages)

    @articles = Rails.cache.fetch("#{cache_key}/archives") do
      Article.all.order('created_at DESC').to_a.slice(page_size * @page_number,
                                                      page_size)
    end

    if @articles.nil?
      @articles = Article.all.order('created_at DESC').to_a \
                          .slice(page_size * @page_number, page_size)
      Rails.cache.write("#{cache_key}/archives", @articles)
    end
  end

  def colophon
  end

  def show
    @article = Rails.cache.fetch("#{cache_key}/#{params[:id]}") do
      Article.friendly.find(params[:id])
    end
  end

  def feed
    @articles = Rails.cache.fetch("#{cache_key}/feed") do
      Article.all.order('created_at DESC, updated_at DESC')
    end

    respond_to do |format|
      format.html
      format.rss { render layout: false }
    end
  end

  private

  def article_params
    params.require(:article).permit(:title, :text)
  end

  def archive_page_number(number_of_pages)
    page_number = params[:page].nil? ? 0 : params[:page].to_i.abs
    page_number = page_number >= 0 ? page_number : 1
    [number_of_pages, page_number].min
  end

  def cache_key
    count = Article.count
    max_updated_at = Article.maximum(:updated_at).try(:utc).try(:to_s, :number)
    "articles/all-#{count}-#{max_updated_at}"
  end
end
{'fraction_non_alphanumeric': 0.11870503597122302, 'fraction_numerical': 0.004796163069544364, 'mean_word_length': 2.949112426035503, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2749682', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1063, 'n_words': 222}