<commit_before>module Kasket
  module WriteMixin

    module ClassMethods
      def remove_from_kasket(ids)
        Array(ids).each do |id|
          Kasket.cache.delete(kasket_key_for_id(id))
        end
      end

      def update_counters_with_kasket_clearing(*args)
        remove_from_kasket(args[0])
        update_counters_without_kasket_clearing(*args)
      end

      def transaction_with_kasket_disabled(*args)
        without_kasket do
          transaction_without_kasket_disabled(*args) { yield }
        end
      end
    end

    module InstanceMethods
      def kasket_key
        @kasket_key ||= new_record? ? nil : self.class.kasket_key_for_id(id)
      end

      def default_kasket_cacheable?
        true
      end

      def store_in_kasket(key = kasket_key)
        if kasket_cacheable? && key
          options = { expires_in: self.class.kasket_ttl } if self.class.kasket_ttl
          Kasket.cache.write(key, attributes_before_type_cast.dup, options)
          key
        end
      end

      def kasket_keys(options = {})
        attribute_sets = [attributes.symbolize_keys]

        previous_changes = options[:previous_changes] || previous_changes()
        if previous_changes.present?
          old_attributes = Hash[*previous_changes.map { |attribute, values| [attribute, values[0]] }.flatten].symbolize_keys
          attribute_sets << old_attributes.reverse_merge(attribute_sets[0])
        end

        keys = []
        self.class.kasket_indices.each do |index|
          keys += attribute_sets.map do |attribute_set|
            key = self.class.kasket_key_for(index.map { |attribute| [attribute, attribute_set[attribute]]})
            index.include?(:id) ? key : [key, key + '/first']
          end
        end

        keys.flatten!
        keys.uniq!
        keys
      end

      def kasket_after_commit
        keys = kasket_keys

        if persisted? && Kasket::CONFIGURATION[:write_through]
          key = store_in_kasket
          keys.delete(key)
        end
<commit_msg>small performance fixes around not symbolizing everything and generating less objects<commit_after>module Kasket
  module WriteMixin

    module ClassMethods
      def remove_from_kasket(ids)
        Array(ids).each do |id|
          Kasket.cache.delete(kasket_key_for_id(id))
        end
      end

      def update_counters_with_kasket_clearing(*args)
        remove_from_kasket(args[0])
        update_counters_without_kasket_clearing(*args)
      end

      def transaction_with_kasket_disabled(*args)
        without_kasket do
          transaction_without_kasket_disabled(*args) { yield }
        end
      end
    end

    module InstanceMethods
      def kasket_key
        @kasket_key ||= new_record? ? nil : self.class.kasket_key_for_id(id)
      end

      def default_kasket_cacheable?
        true
      end

      def store_in_kasket(key = kasket_key)
        if kasket_cacheable? && key
          options = { expires_in: self.class.kasket_ttl } if self.class.kasket_ttl
          Kasket.cache.write(key, attributes_before_type_cast.dup, options)
          key
        end
      end

      def kasket_keys(options = {})
        attribute_sets = [attributes]

        previous_changes = options[:previous_changes] || previous_changes()
        if previous_changes.present?
          old_attributes = previous_changes.each_with_object({}) { |(attribute, (old, _)), all| all[attribute.to_s] = old }
          attribute_sets << old_attributes.reverse_merge(attribute_sets[0])
          attribute_sets << attribute_sets[0].merge(old_attributes)
        end

        keys = []
        self.class.kasket_indices.each do |index|
          keys.concat(
            attribute_sets.map do |attribute_set|
              key = self.class.kasket_key_for(index.map { |attribute| [attribute, attribute_set[attribute.to_s]] })
              index.include?(:id) ? key : [key, key + '/first']
            end
          )
        end

        keys.flatten!
        keys.uniq!
        keys
      end

      def kasket_after_commit
        keys = kasket_keys

        if persisted? && Kasket::CONFIGURATION[:write_through]
          key = store_in_kasket
          keys.delete(key)
        end

{'fraction_non_alphanumeric': 0.08319467554076539, 'fraction_numerical': 0.0014261944378416924, 'mean_word_length': 2.3212312549329126, 'pattern_counts': {'":': 0, '<': 9, '<?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': '7454730', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1242, 'n_words': 236}