<commit_before># Most of the logic here was based on the dalli gem
module ActiveSupport
  module Cache
    class Lru
      attr_reader :silence, :options
      alias_method :silence?, :silence

      def initialize(options)
        @options = options
        @sized_list = SizedList.new @options[:max_size]
        @sized_list.enable_time_based_stats = !! @options[:enable_time_based_stats]
      end

      # Silence the logger.
      def silence!
        @silence = true
        self
      end

      # Silence the logger within a block.
      def mute
        previous_silence, @silence = defined?(@silence) && @silence, true
        yield
      ensure
        @silence = previous_silence
      end

      def fetch(name, options=nil)
        if block_given?
          entry = instrument(:read, name, options) do |payload|
            payload[:super_operation] = :fetch if payload
            read(name, options)
          end

          if !entry.nil?
            instrument(:fetch_hit, name, options) { |payload| }
            entry
          else
            result = instrument(:generate, name, options) do |payload|
              yield
            end
            write(name, result, options)
            result
          end
        else
          read(name, options)
        end
      end

      def read(name, options=nil)
        instrument(:read, name, options) do |payload|
          entry = @sized_list[name]
          payload[:hit] = !!entry if payload
          entry
        end
      end

      def write(name, value, options=nil)
        instrument(:write, name, options) do |payload|
          @sized_list[name] = value
          if payload
            payload[:eviction] = @sized_list.evicted?
            payload[:total_evictions] = @sized_list.evictions
            payload[:eviction_frequency] = @sized_list.eviction_frequency
            payload[:last_time_between_evictions] = @sized_list.last_time_between_evictions
          end
        end
      end

      def exist?(name, options=nil)
        @sized_list.exist? name
      end

      def delete(name, options=nil)
        @sized_list.delete name
      end

      def read_multi(*names)
        {}.tap do |results|
          names.each do |n|
            if v = @sized_list[n]
              results[n] = v
            end
          end
        end
      end

      def increment(name, amount = 1, options=nil)
        if v = @sized_list[name]
          @sized_list[name] = v + amount
        end
      end

      def decrement(name, amount = 1, options=nil)
        if v = @sized_list[name]
          @sized_list[name] = v - amount
        end
      end

      def reset
        @sized_list = SizedList.new @options[:max_size]
      end
      alias clear reset

      protected

      def instrument(operation, key, options=nil)
        log(operation, key, options)

        if ActiveSupport::Cache::Store.instrument
          payload = { :key => key }
          payload.merge!(options) if options.is_a?(Hash)
          ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload){ yield(payload) }
        else
          yield(nil)
        end
      end

      def log(operation, key, options=nil)
        return unless logger && logger.debug? && !silence?
        logger.debug("Cache #{operation}: #{key}#{options.blank? ? "" : " (#{options.inspect})"}")
      end

      def logger
        Rails.logger
      end
    end
  end
end
<commit_msg>added options to reset and clear<commit_after># Most of the logic here was based on the dalli gem
module ActiveSupport
  module Cache
    class Lru
      attr_reader :silence, :options
      alias_method :silence?, :silence

      def initialize(options)
        @options = options
        @sized_list = SizedList.new @options[:max_size]
        @sized_list.enable_time_based_stats = !! @options[:enable_time_based_stats]
      end

      # Silence the logger.
      def silence!
        @silence = true
        self
      end

      # Silence the logger within a block.
      def mute
        previous_silence, @silence = defined?(@silence) && @silence, true
        yield
      ensure
        @silence = previous_silence
      end

      def fetch(name, options=nil)
        if block_given?
          entry = instrument(:read, name, options) do |payload|
            payload[:super_operation] = :fetch if payload
            read(name, options)
          end

          if !entry.nil?
            instrument(:fetch_hit, name, options) { |payload| }
            entry
          else
            result = instrument(:generate, name, options) do |payload|
              yield
            end
            write(name, result, options)
            result
          end
        else
          read(name, options)
        end
      end

      def read(name, options=nil)
        instrument(:read, name, options) do |payload|
          entry = @sized_list[name]
          payload[:hit] = !!entry if payload
          entry
        end
      end

      def write(name, value, options=nil)
        instrument(:write, name, options) do |payload|
          @sized_list[name] = value
          if payload
            payload[:eviction] = @sized_list.evicted?
            payload[:total_evictions] = @sized_list.evictions
            payload[:eviction_frequency] = @sized_list.eviction_frequency
            payload[:last_time_between_evictions] = @sized_list.last_time_between_evictions
          end
        end
      end

      def exist?(name, options=nil)
        @sized_list.exist? name
      end

      def delete(name, options=nil)
        @sized_list.delete name
      end

      def read_multi(*names)
        {}.tap do |results|
          names.each do |n|
            if v = @sized_list[n]
              results[n] = v
            end
          end
        end
      end

      def increment(name, amount = 1, options=nil)
        if v = @sized_list[name]
          @sized_list[name] = v + amount
        end
      end

      def decrement(name, amount = 1, options=nil)
        if v = @sized_list[name]
          @sized_list[name] = v - amount
        end
      end

      def reset(options={})
        @sized_list = SizedList.new @options[:max_size]
      end
      alias clear reset

      protected

      def instrument(operation, key, options=nil)
        log(operation, key, options)

        if ActiveSupport::Cache::Store.instrument
          payload = { :key => key }
          payload.merge!(options) if options.is_a?(Hash)
          ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload){ yield(payload) }
        else
          yield(nil)
        end
      end

      def log(operation, key, options=nil)
        return unless logger && logger.debug? && !silence?
        logger.debug("Cache #{operation}: #{key}#{options.blank? ? "" : " (#{options.inspect})"}")
      end

      def logger
        Rails.logger
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.09271809661139149, 'fraction_numerical': 0.0005767844268204759, 'mean_word_length': 1.8379705400981996, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5502128', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1929, 'n_words': 513}