<commit_before>module ActiveRestClient
  module Caching
    module ClassMethods
      @@perform_caching = true

      def perform_caching(value = nil)
        if value.nil?
          if @perform_caching.nil?
            @@perform_caching
          else
            @perform_caching
          end
        else
          @perform_caching = value
        end
      end

      def perform_caching=(value)
        @@perform_caching = value
        @perform_caching = value
      end

      def cache_store=(value)
        raise InvalidCacheStoreException.new("Cache store does not implement #read") unless value.respond_to?(:read)
        raise InvalidCacheStoreException.new("Cache store does not implement #write") unless value.respond_to?(:write)
        raise InvalidCacheStoreException.new("Cache store does not implement #fetch") unless value.respond_to?(:fetch)
        @@cache_store = value
      end

      def cache_store
        rails_cache_store = if Object.const_defined?(:Rails)
          ::Rails.cache
        else
          nil
        end
        (@@cache_store rescue nil) || rails_cache_store
      end

      def _reset_caching!
        @@perform_caching = false
        @perform_caching = false
        @@cache_store = nil
      end

      def read_cached_response(request)
        if cache_store
          key = "#{request.class_name}:#{request.url}"
          cache_store.read(key)
        end
      end

      def write_cached_response(request, response, result)
        response.headers.keys.select{|h| h.is_a? String}.each do |key|
          response.headers[key.downcase.to_sym] = response.headers[key]
        end
        if cache_store && (response.headers[:etag] || response.headers[:expires])
          key = "#{request.class_name}:#{request.url}"
          cached_response = CachedResponse.new(status:response.status, result:result)
          cached_response.etag = response.headers[:etag] if response.headers[:etag]
          cached_response.expires = Time.parse(response.headers[:expires]) if response.headers[:expires]
          cache_store.write(key, cached_response, {})
        end
      end
    end

    def self.included(base)
      base.extend(ClassMethods)
    end

  end

  class CachedResponse<commit_msg>Adding logging to cache writing<commit_after>module ActiveRestClient
  module Caching
    module ClassMethods
      @@perform_caching = true

      def perform_caching(value = nil)
        if value.nil?
          if @perform_caching.nil?
            @@perform_caching
          else
            @perform_caching
          end
        else
          @perform_caching = value
        end
      end

      def perform_caching=(value)
        @@perform_caching = value
        @perform_caching = value
      end

      def cache_store=(value)
        raise InvalidCacheStoreException.new("Cache store does not implement #read") unless value.respond_to?(:read)
        raise InvalidCacheStoreException.new("Cache store does not implement #write") unless value.respond_to?(:write)
        raise InvalidCacheStoreException.new("Cache store does not implement #fetch") unless value.respond_to?(:fetch)
        @@cache_store = value
      end

      def cache_store
        rails_cache_store = if Object.const_defined?(:Rails)
          ::Rails.cache
        else
          nil
        end
        (@@cache_store rescue nil) || rails_cache_store
      end

      def _reset_caching!
        @@perform_caching = false
        @perform_caching = false
        @@cache_store = nil
      end

      def read_cached_response(request)
        if cache_store
          key = "#{request.class_name}:#{request.url}"
          cache_store.read(key)
        end
      end

      def write_cached_response(request, response, result)
        response.headers.keys.select{|h| h.is_a? String}.each do |key|
          response.headers[key.downcase.to_sym] = response.headers[key]
        end
        if cache_store && (response.headers[:etag] || response.headers[:expires])
          key = "#{request.class_name}:#{request.url}"
          ActiveRestClient::Logger.debug "  \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{key} - Writing to cache"
          cached_response = CachedResponse.new(status:response.status, result:result)
          cached_response.etag = response.headers[:etag] if response.headers[:etag]
          cached_response.expires = Time.parse(response.headers[:expires]) if response.headers[:expires]
          cache_store.write(key, cached_response, {})
        end
      end
    end

    def self.included(base)
      base.extend(ClassMethods)
    end

  end

  class CachedResponse
{'fraction_non_alphanumeric': 0.09173517957594116, 'fraction_numerical': 0.002379922111639983, 'mean_word_length': 2.4244444444444446, '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': '1862735', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1280, 'n_words': 281}