<commit_before>module CachedCounts
  class Cache
    def initialize(scope)
      @scope = scope
    end

    def count(*args)
      cached_count || uncached_count(*args)
    end

    # Clear out any count caches which have SQL that includes the scopes table
    def clear
      keys = all_keys
      invalid_keys = keys.select { |key| key.include?(@scope.table_name.downcase.singularize) }
      invalid_keys.each { |key| Rails.cache.delete(key) }

      Rails.cache.write(list_key, keys - invalid_keys)
    end

    private

    def cached_count
      Rails.cache.fetch(current_key)
    end

    def uncached_count(*args)
      @scope.count_without_caching(*args).tap do |count|
        Rails.cache.write(current_key, count)<commit_msg>Ensure when writing the list of keys, we have the most upto date list<commit_after>module CachedCounts
  class Cache
    def initialize(scope)
      @scope = scope
    end

    def count(*args)
      cached_count || uncached_count(*args)
    end

    # Clear out any count caches which have SQL that includes the scopes table
    def clear
      invalid_keys = all_keys.select { |key| key.include?(@scope.table_name.downcase.singularize) }
      invalid_keys.each { |key| Rails.cache.delete(key) }

      Rails.cache.write(list_key, all_keys - invalid_keys)
    end

    private

    def cached_count
      Rails.cache.fetch(current_key)
    end

    def uncached_count(*args)
      @scope.count_without_caching(*args).tap do |count|
        Rails.cache.write(current_key, count)
{'fraction_non_alphanumeric': 0.0870712401055409, 'fraction_numerical': 0.0, 'mean_word_length': 2.9816272965879267, 'pattern_counts': {'":': 0, '<': 3, '<?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': '893284', 'n_tokens_mistral': 486, 'n_tokens_neox': 467, 'n_words': 118}